Search code examples
phpzend-frameworkjoinzend-dbzend-db-table

Join in Zend Framework using alias


How to do the query:

$name = $this->_dbTable->info('name');
$result = $this->_dbTable->select()->from($name)->setIntegrityCheck(false);
$result->join(array('t' => "$name.tipo")), "t.id = $name.id");

Where '$name.tipo' is the table name for join.


Solution

  • You can't join to a different table per row, depending on the value of tipo. SQL requires that the tables be fixed at the time you prepare the query, the table can't change as the data values fetched from a given column are discovered.

    Your design is called Polymorphic Associations, and it's a non-relational design. It's very awkward to do this in SQL.

    See my presentation Practical Object Oriented Models In SQL for some alternatives.