Exemple #1
0
    /**
     * Self Referencing
     */
    function self_referencing()
    {
        echo '<h1>Self Referencing Relationships</h1>';
        echo '<p>DataMapper allows you to have a model setup with a relationship to its self (hence, self referencing).</p>';
        echo '<p>Take a look at the Employee, Manager, Supervisor, and Underling models included in the DataMapper download.  You\'ll notice they all have references back to the employees table (with their inheritance of the Employee model). The relationships they\'re setup with are:</p>';
        echo '<ul><li>A Manager manages Many Supervisors.</li><li>A Supervisor has One Manager.</li><li>A Supervisor supervises Many Underlings.</li><li>An Underling has One Supervisor.</li></ul>';
        echo '<hr />';
        echo '<p>Here we create a number of different types of employees.</p>';
        echo '<code>
		// Create Manager<br />
		$m = new Manager();<br />
		$m->first_name = \'Jake\';<br />
		$m->last_name = \'Ronalds\';<br />
		$m->position = \'Manager\';<br />
		$m->save();<br />
		<br />
		<br />
		// Create Supervisors<br />
		$s = new Supervisor();<br />
		$s->first_name = \'Bob\';<br />
		$s->last_name = \'Thomas\';<br />
		$s->position = \'Supervisor\';<br />
		$s->save();<br />
		<br />
		$s = new Supervisor();<br />
		$s->first_name = \'Sarah\';<br />
		$s->last_name = \'Parker\';<br />
		$s->position = \'Supervisor\';<br />
		$s->save();<br />
		<br />
		<br />
		// Create Underlings<br />
		$u = new Underling();<br />
		$u->first_name = \'Fred\';<br />
		$u->last_name = \'Smith\';<br />
		$u->position = \'Underling\';<br />
		$u->save();<br />
		<br />
		$u = new Underling();<br />
		$u->first_name = \'Jayne\';<br />
		$u->last_name = \'Doe\';<br />
		$u->position = \'Underling\';<br />
		$u->save();<br />
		<br />
		$u = new Underling();<br />
		$u->first_name = \'Joe\';<br />
		$u->last_name = \'Public\';<br />
		$u->position = \'Underling\';<br />
		$u->save();<br />
		<br />
		$u = new Underling();<br />
		$u->first_name = \'Sam\';<br />
		$u->last_name = \'Rogers\';<br />
		$u->position = \'Underling\';<br />
		$u->save();</code>';
        // Create Manager
        $m = new Manager();
        $m->first_name = 'Jake';
        $m->last_name = 'Ronalds';
        $m->position = 'Manager';
        $m->save();
        // Create Supervisors
        $s = new Supervisor();
        $s->first_name = 'Bob';
        $s->last_name = 'Thomas';
        $s->position = 'Supervisor';
        $s->save();
        $s = new Supervisor();
        $s->first_name = 'Sarah';
        $s->last_name = 'Parker';
        $s->position = 'Supervisor';
        $s->save();
        // Create Underlings
        $u = new Underling();
        $u->first_name = 'Fred';
        $u->last_name = 'Smith';
        $u->position = 'Underling';
        $u->save();
        $u = new Underling();
        $u->first_name = 'Jayne';
        $u->last_name = 'Doe';
        $u->position = 'Underling';
        $u->save();
        $u = new Underling();
        $u->first_name = 'Joe';
        $u->last_name = 'Public';
        $u->position = 'Underling';
        $u->save();
        $u = new Underling();
        $u->first_name = 'Sam';
        $u->last_name = 'Rogers';
        $u->position = 'Underling';
        $u->save();
        echo '<hr />';
        echo '<p>Now we\'ll set up some relationships.</p>';
        echo '<code>// Get the first Supervisor<br />
		$s = new Supervisor();<br />
		$s->get(1);<br />
		<br />
		// Get first 2 Underlings<br />
		$u = new Underling();<br />
		$u->get(2);<br />
		<br />
		// Setup Supervisor to supervise those Underlings<br />
		$s->save($u->all);<br />
		<br />
		<br />
		// Get the second Supervisor<br />
		$s = new Supervisor();<br />
		$s->get(1, 1);<br />
		<br />
		// Get the other 2 Underlings<br />
		$u = new Underling();<br />
		$u->get(2, 2);<br />
		<br />
		// Setup Supervisor to supervise those Underlings<br />
		$s->save($u->all);<br />
		<br />
		<br />
		// Get the Manager<br />
		$m = new Manager();<br />
		$m->get();<br />
		<br />
		// Get the Supervisors<br />
		$s = new Supervisor();<br />
		$s->get();<br />
		<br />
		// Setup Manager to manage those Supervisors<br />
		$m->save($s->all);</code>';
        // Get the first Supervisor
        $s = new Supervisor();
        $s->get(1);
        // Get first 2 Underlings
        $u = new Underling();
        $u->get(2);
        // Setup Supervisor to supervise those Underlings
        $s->save($u->all);
        // Get the second Supervisor
        $s = new Supervisor();
        $s->get(1, 1);
        // Get the other 2 Underlings
        $u = new Underling();
        $u->get(2, 2);
        // Setup Supervisor to supervise those Underlings
        $s->save($u->all);
        // Get the Manager
        $m = new Manager();
        $m->get();
        // Get the Supervisors
        $s = new Supervisor();
        $s->get();
        // Setup Manager to manage those Supervisors
        $m->save($s->all);
        echo '<hr />';
        echo '<p>Now that we\'ve got our relationships setup, let\'s show how they\'re related to each other:</p>';
        echo '<code>$m = new Manager();<br />
		$m->get();<br />
		<br />
		echo $m->full_name() . \' is a \' . $m->position . \' who manages these Supervisors:&lt;br /&gt;\';<br />
		<br />
		$m->supervisor->get();<br />
		<br />
		foreach ($m->supervisor->all as $s)<br />
		{
		&nbsp;&nbsp;&nbsp;&nbsp;echo \'&nbsp;&nbsp;&nbsp;&nbsp;\' . $s->full_name() . \' is a \' . $s->position . \' who supervises these Underlings:&lt;br /&gt;\';<br />
			<br />
		&nbsp;&nbsp;&nbsp;&nbsp;$s->underling->get();<br />
			<br />
		&nbsp;&nbsp;&nbsp;&nbsp;foreach ($s->underling->all as $u)<br />
		&nbsp;&nbsp;&nbsp;&nbsp;{<br />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\' . $u->full_name() . \' is an \' . $u->position . \'&lt;br /&gt;\';<br />
		&nbsp;&nbsp;&nbsp;&nbsp;}<br />
		}</code>';
        echo '<hr />';
        echo '<p>This produces:</p>';
        $m = new Manager();
        $m->get();
        echo $m->full_name() . ' is a ' . $m->position . ' who manages these Supervisors:<br />';
        $m->supervisor->get();
        foreach ($m->supervisor->all as $s) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;' . $s->full_name() . ' is a ' . $s->position . ' who supervises these Underlings:<br />';
            $s->underling->get();
            foreach ($s->underling->all as $u) {
                echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $u->full_name() . ' is an ' . $u->position . '<br />';
            }
        }
        echo '<hr />';
        echo '<p>If we wanted to look at Sam Rogers\' supervisor and their supervisors manager, we could do:</p>';
        echo '<code>$u = new Underling();<br />
		$u->where(\'first_name\', \'Sam\')->where(\'last_name\', \'Rogers\')->get();<br />
		<br />
		$u->supervisor->get();<br />
		$u->supervisor->manager->get();<br />
		<br />
		echo $u->full_name . \' is supervised by \' . $u->supervisor->full_name() . \' who is in turn managed by \' . $u->supervisor->manager->full_name() . \'&lt;br /&gt;\';</code>';
        echo '<hr />';
        echo '<p>This produces:</p>';
        $u = new Underling();
        $u->where('first_name', 'Sam')->where('last_name', 'Rogers')->get();
        $u->supervisor->get();
        $u->supervisor->manager->get();
        echo $u->full_name() . ' is supervised by ' . $u->supervisor->full_name() . ' who is in turn managed by ' . $u->supervisor->manager->full_name() . '<br />';
        echo '<hr />';
        echo '<p>Ok, so let\'s do something different and show the total number of Managers:</p>';
        echo '<code>echo $m . \' count: \' . $m->count() . \'&nbsp;br /&nbsp;\';</code>';
        echo '<hr />';
        echo '<p>This produces:</p>';
        echo $m . ' count: ' . $m->count() . '<br />';
        echo '<hr />';
        echo '<p>Now let\'s show how many supervisors are related to ' . $m . ' ' . $m->full_name() . ':</p>';
        echo '<code>echo $m . \' \' . $m->full_name() . \' supervises \' .  $m->supervisor->count() . \' \' . plural($m->supervisor) . \'&lt;br /&gt;\' ;</code>';
        echo '<hr />';
        echo '<p>This produces:</p>';
        echo $m . ' ' . $m->full_name() . ' manages ' . $m->supervisor->count() . ' ' . plural($m->supervisor) . '.<br />';
        echo '<hr />';
        echo '<p>The total counts:</p>';
        echo ucfirst(plural($m)) . ': ' . $m->count() . '<br />';
        echo ucfirst(plural($s)) . ': ' . $s->count() . '<br />';
        echo ucfirst(plural($u)) . ': ' . $u->count() . '<br />';
        echo '<p><a href="' . site_url('examples') . '">Back to Examples</a></p>';
    }