I came to a sort of a basic problem and I can’t find a solution the whole day now.
I have model A that HAS_MANY B and relation is ‘bs’. How can I add new object to the list without saving them in database?
The thing is user fills out the form and I create a model from it, but I can’t attach other objects to that model so I can save them later or do anything with them, using A->bs only works when reading from database.
A->bs[] = $b; - doesn’t work, PHP gives me an error that it has no effect
A->addRelatedRecord() - doesn’t also help, it can only add new relations, not change existing ones
How do you do it? If object B is contained within A, how do you connect the 2 without saving them in database first?
I have written a AR extension that can do this, but it only works with MANY_MANY so far.
i still have to do some tests and enhancements, but i think i will release this enhancement collection as a behaviour in february. Handling HAS_ONE and HAS_MANY relations shouldn´t be a problem, since MANY_MANY is the most complex
I am suprised AR doesn’t have that already. I know I could save related unsaved (database) objects to some attribute in AR object and then later use it to actualy save them to database or whatever, but I can’t believe Yii doesn’t have this covered.
Oh well… I’ll do something like that and see what happens.
If anyone has an idea or own solution I would be happy to hear it.