查找行“a”是否与ORM中的行“b”有关的最有效方法?

This is my problem:

  • I have a 1-N relationship between SomeTable and SomeOtherTable.
  • User wants to modify row b, but he can only modify it if row a has a relationship with row b
  • Therfore, I need to find if row a from SomeTable has a relationship with row b from SomeOtherTable

If you can, provide a general answer and then a specific answer for symfony 1.4 and doctrine. That would be useful for more people and for myself who works in multiple frameworks.

I need this to handle permissions on the CRUD of some tables.

I find that this is a very recurrent problem

Maybe there is a pattern or a plugin that solves this problem?

  • How do you take the most advantage of symfony caching system?

Currently i just came up with this:

$someRow = Doctrine_Query::create()->from('SomeTable')->
  where('id = ?', $id_someTable)->
  andWhere('id_relation = ?',$id_someOtherTable)->execute();
return $someRow->count() > 0;

For some reason i find this ugly...

Your title is more explicit than your text, anyway.

You have a function called getRelations in Table.php. So you can retrieve all relations to an object and then, make what ever you want with the result.

/**
 * Retrieves all relation objects defined on this table.
 *
 * @return array
 */
public function getRelations()
{
    return $this->_parser->getRelations();
}

So :

$relations = Doctrine_Core::getTable('SomeTable')->getRelations();

For more detail, here is the parser getRelations method.

Edit:

And if you want to try for a given relation, you can then use hasRelation.