php - Which Doctrine association should I use? -



php - Which Doctrine association should I use? -

i've 2 tables:

create table server ( serverid int ) create table resource ( resourceid int auto_increment not null, serverid int not null ) alter table resource add together foreign key (serverid) references server(serverid);

in server entity, i'd getresourcelist() returns list of resource entities.

what right annotations this, , why?

thanks

i suppose utilize doctrine2 php annotations. tables rewritten follows:

your server class annotations:

/** * @id @column(type="integer") * @generatedvalue */ private $id; /** * @var doctrine\common\collections\arraycollection * @onetomany(targetentity="resource", mappedby="server") */ private $resources; public function getid() { homecoming $this->id; } public function getresources() { homecoming $this->resources; } public function addresource(resource $resource){ $this->resources->add($resource); } public function removeresource(resource $resource){ $this->resources->removeelement($resource); }

your resource class annotations:

/** * @id @column(type="integer") * @generatedvalue */ private $id; /** * @var server * @manytoone(targetentity="resource", inversedby="resources") */ private $server; public function getid() { homecoming $this->id; } public function getserver() { homecoming $this->server; } public function setserver(server $server) { $this->server = $server; }

there "one many" association on server side since mapping many resources 1 server. on other side of relationship - logically - there has "many one" association.

now after calling $server->getresources() method arraycollection of associated resource entities.

php doctrine2

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -