Deep Association Mapping in Doctrine

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Deep Association Mapping in Doctrine

tridem
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Deep Association Mapping in Doctrine

Marco Pivetta
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 <[hidden email]> 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 &mdash; 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: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Deep Association Mapping in Doctrine

tridem
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Deep Association Mapping in Doctrine

Marco Pivetta
You will have to use the second form if you remove the reference I've marked
as deprecated...
Anyway, I still think it is worth to do so because you get a cleaner
model...

I didn't validate the schema I wrote, so please check it carefully and pass
it to the Doctrine2 CLI tools...

If you need specific support for Doctrine or wish to continue talking about
it, I would suggest you to use the correct mailing list, where you can get
support by D2 authors ;)

Marco Pivetta
@Ocramius <http://twitter.com/Ocramius>
http://marco-pivetta.com



On 21 April 2011 16:07, tridem-zend <[hidden email]> wrote:

> Wow Marco,
> thanks for this amazing answer. I will have the chance to try your code
> after easter!
>
> One question in advance:
> Will I be able to use a self-refering category like
>
>
> $contact = ....;
> foreach ($contact->getCategories() as $category) {
>    echo $category->getId();
>    echo $category->getName();
> }
>
>
> or do I have to call
>
>
> $contact = ....;
> foreach ($contact->getCategories() as $category) {
>    echo $category->getCategory()->getName();
>    echo $category->getCategory()->getId();
> }
>
>
> ?!
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Deep-Association-Mapping-in-Doctrine-tp3465770p3465997.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>