public function addToQuery($query, $tablename = '', $fieldaliasprefix = '', &$record, $level = 0, $mode = '') { if ($this->createDestination()) { if ($mode != 'update' && $mode != 'add') { if ($this->hasFlag(self::AF_ONETOONE_LAZY)) { if ($this->m_refKey == '') { parent::addToQuery($query, $tablename, $fieldaliasprefix, $record, $level, $mode); return; } } if ($tablename != '') { $tablename .= '.'; } if ($this->m_refKey != '') { // Foreign key is in the destination node. $condition = $tablename . $this->m_ownerInstance->m_primaryKey[0] . '=' . $fieldaliasprefix . $this->fieldName() . '.' . $this->m_refKey; } else { // Foreign key is in the source node $condition = $tablename . $this->fieldName() . '=' . $fieldaliasprefix . $this->fieldName() . '.' . $this->m_destInstance->m_primaryKey[0]; } $condition .= $this->getDestinationFilterCondition($fieldaliasprefix); $query->addJoin($this->m_destInstance->m_table, $fieldaliasprefix . $this->fieldName(), $condition, true); // we pass true as the last param to addToQuery, because we need all fields.. $this->m_destInstance->addToQuery($query, $fieldaliasprefix . $this->fieldName(), $level + 1, true, $mode); } // When storing, we don't add to the query.. we have our own store() method.. // With one exception. If the foreign key is in the source node, we also need to update // the refkey value. if ($this->m_refKey == '' && $mode == 'add') { $query->addField($this->fieldName(), $record[$this->fieldName()][$this->m_destInstance->m_primaryKey[0]], '', '', !$this->hasFlag(self::AF_NO_QUOTES)); } } }
public function addToQuery($query, $tablename = '', $fieldaliasprefix = '', &$record, $level = 0, $mode = '') { if ($this->hasFlag(self::AF_MANYTOONE_LAZY)) { parent::addToQuery($query, $tablename, $fieldaliasprefix, $record, $level, $mode); return; } if ($this->createDestination()) { if ($mode != 'update' && $mode != 'add') { $alias = $fieldaliasprefix . $this->fieldName(); $query->addJoin($this->m_destInstance->m_table, $alias, $this->getJoinCondition($query, $tablename, $alias), $this->m_leftjoin); $this->m_destInstance->addToQuery($query, $alias, $level + 1, false, $mode, $this->m_listColumns); } else { for ($i = 0, $_i = count($this->m_refKey); $i < $_i; ++$i) { if ($record[$this->fieldName()] === null) { $query->addField($this->m_refKey[$i], 'NULL', '', '', false); } else { $value = $record[$this->fieldName()]; if (is_array($value)) { $fk = $this->m_destInstance->getAttribute($this->m_destInstance->m_primaryKey[$i]); $value = $fk->value2db($value); } $query->addField($this->m_refKey[$i], $value, '', '', !$this->hasFlag(self::AF_NO_QUOTES)); } } } } }