public function __get($name) { if ($name === 'primary_key' && !isset($this->primary_key)) { $this->primary_key = [Table::load($this->class_name)->pk[0]]; } return $this->{$name}; }
/** * Returns the {@link Table} object for this model. * * Be sure to call in static scoping: static::table() * * @return Table */ public static function table() { return Table::load(get_called_class()); }
public function set_up($connection_name = null) { parent::set_up($connection_name); $this->sql = new SQLBuilder($this->conn, $this->table_name); $this->table = Table::load($this->class_name); }
/** * Creates INNER JOIN SQL for associations. * * @param Table $from_table the table used for the FROM SQL statement * @param bool $using_through is this a THROUGH relationship? * @param string $alias a table alias for when a table is being joined twice * @return string SQL INNER JOIN fragment */ public function constructInnerJoinSql(Table $from_table, $using_through = false, $alias = null) { if ($using_through) { $join_table = $from_table; $join_table_name = $from_table->getFullyQualifiedTableName(); $from_table_name = Table::load($this->class_name)->getFullyQualifiedTableName(); } else { $join_table = Table::load($this->class_name); $join_table_name = $join_table->getFullyQualifiedTableName(); $from_table_name = $from_table->getFullyQualifiedTableName(); } // need to flip the logic when the key is on the other table if ($this instanceof HasMany || $this instanceof HasOne) { $this->setKeys($from_table->class->getName()); if ($using_through) { $foreign_key = $this->primary_key[0]; $join_primary_key = $this->foreign_key[0]; } else { $join_primary_key = $this->foreign_key[0]; $foreign_key = $this->primary_key[0]; } } else { $foreign_key = $this->foreign_key[0]; $join_primary_key = $this->primary_key[0]; } if (!\is_null($alias)) { $aliased_join_table_name = $alias = $this->getTable()->conn->quoteName($alias); $alias .= ' '; } else { $aliased_join_table_name = $join_table_name; } return "INNER JOIN {$join_table_name} {$alias}ON({$from_table_name}.{$foreign_key} = {$aliased_join_table_name}.{$join_primary_key})"; }
protected function setKeys($model_class_name, $override = false) { //infer from class_name if (!$this->foreign_key || $override) { $this->foreign_key = [Inflector::instance()->keyify($model_class_name)]; } if (!$this->primary_key || $override) { $this->primary_key = Table::load($model_class_name)->pk; } }