You're talking about Doctrine2 right?
I'll use AnnotationMapping to be a little more verbose (hope everything is
correct):
*/**
* @Entity
* @Table(name="contacts")
*/
class Contact {
/**
* @Id
*/
protected $id;
/**
* @Column(type="string")
*/
protected $name;
/**
* @OneToMany(targetEntity="ContactCategory", mappedBy="contact")
* @var \Doctrine\Common\Collections\Collection
*/
protected $categories;
}
/**
* @Entity
* @Table(name="contacts_categories")
*/
class Category {
/**
* @Id
*/
protected $id;
/**
* @Column(type="string")
*/
protected $name;
/**
* @OneToMany(targetEntity="ContactCategory", mappedBy="category")
* @var \Doctrine\Common\Collections\Collection
* @deprecated this is not needed as we already know the category
through a join with categoryOption
*/
protected $contacts;
/**
* @OneToMany(targetEntity="CategoryOption", mappedBy="category")
* @var \Doctrine\Common\Collections\Collection
*/
protected $options;
}
/**
* @Entity
* @Table(name="contacts_categories_options")
*/
class CategoryOption {
/**
* @Id
*/
protected $id;
/**
* @Column(type="string")
*/
protected $label;
/**
* @ManyToOne(targetEntity="Category", inversedBy="options")
* @JoinColumn(name="category_id", referencedColumnName="id")
* @var Category
*/
protected $category;
/**
* @OneToMany(targetEntity="ContactCategory", mappedBy="categoryOption")
* @var \Doctrine\Common\Collections\Collection
*/
protected $categories;
}
/**
* @Entity
* @Table(name="contacts_categories_values")
*/
class ContactCategory {
/**
* @Id
*/
protected $id;
/**
* @ManyToOne(targetEntity="CategoryOption", inversedBy="contacts")
* @JoinColumn(name="option_id", referencedColumnName="id")
* @var CategoryOption
*/
protected $categoryOption;
/**
* @ManyToOne(targetEntity="Category", inversedBy="contacts")
* @JoinColumn(name="category_id", referencedColumnName="id")
* @var Category
* @deprecated this is not needed as we already know the category
through a join with categoryOption
*/
protected $category;
/**
* @ManyToOne(targetEntity="Contact", inversedBy="categories")
* @JoinColumn(name="contact_id", referencedColumnName="id")
* @var Contact
*/
protected $contact;
}*
As you see, I've marked some stuff as @deprecated as I think you should drop
some columns.
Supposing you got getters and setters for everything, to fetch users in a
category, you should traverse like this way:
foreach($category->getOptions() as $option) {
$foundUsers[] = $option->getContact();
}
Don't worry, this won't bomb your database as you'll be able to hydrate
joined entities with a DQL like this:
SELECT c, o, p FROM Category c JOIN c.options o JOIN o.user
That way, fetched categories will already be populated with users and
options :D
The same happens from users to categories.
Hope it helped you and also hope it's what you were asking for...
Marco Pivetta
@Ocramius <http://twitter.com/Ocramius>
http://marco-pivetta.com
On 21 April 2011 14:10, tridem-zend <[email protected]> wrote:
> My topic is pretty much Doctrine relatetd. But since it will be a big part
> of
> ZF 2.0 I hope someone can help me here.
>
> I have a main entity "Contact". This contact belongs to multiple
> "Categories".
> The Categories have several options which are options and labels of a
> select
> in my contact form.
>
> These are my tables:
>
> contacts
> - id
> - name
> - categories
>
> contacts_categories
> - id
> - name
>
> contacts_categories_options
> - id
> - category_id
> - label
>
> contacts_categories_values
> - id
> - contact_id
> - category_id
> - option_id
>
> How do I map this in doctrine? Do I need self-reference on
> "contacts_categories_values" on a category in "contacts_categories"?
>
> Afterwards, do I get single objects when I call $contact->getCategories()
> only including id, name from the parent table or do I have to call
> $contact->getCategories and the getCategory on each object?
>
> Thanks in advance!
>
>
> http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html
> 5. Association Mapping — Doctrine 2 ORM v2.0.0 documentation
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Deep-Association-Mapping-in-Doctrine-tp3465770p3465770.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [email protected]
> Info: http://framework.zend.com/archives
> Unsubscribe: [email protected]
>
>
>