Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several ways that <a href="http://gacelaphp.com/documentation/usage/relationships-advanced/" rel="nofollow">Gacela</a> can handle automatically fetching related information:</p> <p>1) Inheritance relationship</p> <p>In an example like the following:</p> <pre><code>CREATE TABLE wizards ( wizardId INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, fname VARCHAR(255) NOT NULL, lname VARCHAR(255) NOT NULL, ROLE ENUM('teacher', 'student') NULL, addressId INT UNSIGNED NULL, CONSTRAINT fk_address_wizard FOREIGN KEY (addressId) REFERENCES addresses(addressId) ON DELETE SET NULL ) ENGINE = Innodb; CREATE TABLE students ( wizardId INT UNSIGNED NOT NULL PRIMARY KEY, houseId INT UNSIGNED NOT NULL, isDAMembmer BOOL NOT NULL DEFAULT 0, CONSTRAINT fk_wizard_student FOREIGN KEY (wizardId) REFERENCES wizards(wizardId) ON DELETE CASCADE, CONSTRAINT fk_house_students FOREIGN KEY (houseId) REFERENCES houses(houseId) ON DELETE RESTRICT ) ENGINE = Innodb; </code></pre> <p>The <code>students</code> table has the same primary key as the <code>wizards</code> table and because of the foreign key relationship definition, Gacela will detect that <code>students</code> inherits all of the fields from <code>wizards</code>.</p> <p>2) Dependent relationship</p> <p>This is probably closer to what you're looking for:</p> <pre><code>CREATE TABLE addresses ( addressId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, locationName VARCHAR(255) NOT NULL ) ENGINE = Innodb; CREATE TABLE wizards ( wizardId INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, fname VARCHAR(255) NOT NULL, lname VARCHAR(255) NOT NULL, ROLE ENUM('teacher', 'student') NULL, addressId INT UNSIGNED NULL, CONSTRAINT fk_address_wizard FOREIGN KEY (addressId) REFERENCES addresses(addressId) ON DELETE SET NULL ) ENGINE = Innodb; </code></pre> <p>However, this example still differs slightly from your example because the addressId is in the <code>wizards</code> table thus creating a belongsTo relationship rather than a hasMany which is what your example reflects. </p> <p>There is a third option available in Gacela, however I would first suggest that you consider that while it is often preferable to fetch data eagerly, there are real performance implications in always fetching eagerly rather than lazy loading data. It is for this reason that Gacela (and basically every other Data Mapper or ORM) by default does not eagerly fetch data from hasMany relationships. That said, you could write something like the following:</p> <pre><code>class Mapper extends \Gacela\Mapper { public function find($id) { $query = $this-&gt;_source()-&gt;query(); $query-&gt;from('persons', array('fullName')) -&gt;join('phones', 'phones.person=persons.id', array('phonenumber'), 'left') -&gt;where('persons.id = :id', array(':id' =&gt; $id); $data = $this-&gt;_source-&gt;query($query); return $this-&gt;_load($data); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload