burgua
burgua

Reputation: 88

One to many connection in Doctrine

I have tried to create one-to-many connection, but it works very strange.

I suspect that class User has one Country, and class Country has many Users. But User->Country return ever an array with one Country, (Doctrine Collection, not a Record).

Have anyone idea why?

Upvotes: 1

Views: 923

Answers (1)

prodigitalson
prodigitalson

Reputation: 60413

I need CountryUser object, and I know, that such relation can be made without additional object.

But that is the answer to your question. Youre setting it up as a many-to-many by using the refClass as such youre always goign to get a Doctrine_Collection when accessing the relation. This is how it works, im not sure why you would expect a single object.

You might be able to override the accessor to only return a single record if there is only one in the collection, but this is most likely going to break things because everything is going to be expecting a Doctine_Collection to be returned from this accessor:

class User extends sfDoctrineRecord {
    public function getCountry(
       $coll = $this->_get('Country');
        if($coll->count() == 1){
          return $coll->getFirst();
        }
        else
        {
          return $coll;
        }
    }
}

The real solution is to use the proper type of relationship...

Also... if youre using Symfony why in gods name are you not using the YAML definitions? Its so much easier to use and manage.

Upvotes: 1

Related Questions