0

I'm using symfony 1.4 with Doctrine ORM. I have two classes related by a one-to-many relationship, and I have the object of the "one side". I need to associate this object , let's say A, to the "many side" object, let's say B. The thing is A have not a method to add just one object to its Doctrine_Collection property, but instead it has a setter that receives a Doctrine_Collection. How can I achieve this association?

I have something like:

$a = new A();

$b = new B(); // Obviously, $b is not new, this is just for reference

$a->setB($b); // This is what I can't do. A::setB() receives a 
              // Doctrine_Collection of objects of type B, and I don't
              // know how to build it.

Please, any help would be really appreciated!!! thanks!

EDIT:

Maybe I didn't explained me well. My relationships are properly setted. I'm not trying to retrieve any object; instead, I'm trying to set the object. The problem is that the method A::setB() expects a Doctrine_Collection as a parameter, instead of the object itself. I don't know how to build that Doctrine_Collection, and that's what I'm asking here... I just need to add $b to the Doctrine_Collection of the related objects of $a.

j0k
  • 22,600
  • 28
  • 79
  • 90
Throoze
  • 3,988
  • 8
  • 45
  • 67

2 Answers2

2

If you need to just build a Doctrine_Collection, you can do so as follows:

// here, 'B' is the type of objects in the collection
$collection = new Doctrine_Collection("B");
$collection->add($b);

$a->setB($collection);

You can also do:

$a->b[] = $b;

and, as far as I know, Doctrine will add the relationship if it doesn't already exist, and ignore it if it does.

richsage
  • 26,912
  • 8
  • 58
  • 65
  • WOW, that's exactly what I was looking for! just one thing, what is AFAIK? Sorry, but I'm not native English speaker, and I'm not sure if it is an English word or a technical Doctrine-related word... Thank you very much! =) – Throoze Dec 23 '11 at 19:32
  • heh :-) no, it just means "as far as I know". I'll edit to make it clearer ;-) – richsage Dec 23 '11 at 19:37
1

I feel like this should be possible in a way to similar to what you are describing, but I never had to. First, let me just state how I understand your question: You have a one to many relationship between 2 models. To make this easier to read, I will call the one side Categories and the many Posts. You want the be able to assign a doctrine collection of posts to the category by a function call, like $category->add($posts).

Most people end up doing this from the many side, and not the one side. That is, we are used to using $post->setCategory($category).

Personally, I would just look through the posts and assign category to each one. If you find the need to do this in a few places, you should probably make a custom function in your category class to handle this.

MrGlass
  • 9,094
  • 17
  • 64
  • 89
  • That's exactly my question!. I have done something similar to what you suggest, but I have not tested it, so I'm not sure if it works. My doubt is: when you assign a Category to a post, the Posts Doctrine_Collection on the Category object gets updated simultaneously? – Throoze Dec 23 '11 at 19:28
  • Ah, interesting. Well, the doctrine collection is the collection of results to your last query, so i doubt it would update with changes to that object. You would need to run the query after you assign the posts to the category. – MrGlass Dec 24 '11 at 23:05
  • I thought so... Thanks anyways! Take a look to richsage's answer... =) – Throoze Dec 26 '11 at 08:02