/** * Constructor * Use $db->createResult( $parent, $name ) instead * * @param Database|Result|Row $parent * @param string $name */ function __construct($parent, $name) { if ($parent instanceof Database) { // basic result $this->db = $parent; $this->table = $this->db->getAlias($name); } else { // Row or Result // result referenced to parent $this->parent_ = $parent; $this->db = $parent->getDatabase(); // determine type of reference based on conventions and user hints $fullName = $name; $name = preg_replace('/List$/', '', $fullName); $this->table = $this->db->getAlias($name); $this->single = $name === $fullName; if ($this->single) { $this->key = $this->db->getPrimary($this->getTable()); $this->parentKey = $this->db->getReference($parent->getTable(), $name); } else { $this->key = $this->db->getBackReference($parent->getTable(), $name); $this->parentKey = $this->db->getPrimary($parent->getTable()); } } }
/** * Create a back reference * * @param $key * * @return $this * * @since 1.0.0 */ public function via($key) { if ($this->parent instanceof DatabaseTable) { $this->db->schema()->setReference($this->parent->getTable(), $this->table, $key); } return $this; }