require_once dirname(__FILE__) . '/../../YDFramework2/YDF2_init.php'; YDConfig::set('YD_DATABASEOBJECT_PATH', YD_SELF_DIR . YD_DIRDELIM . 'includes'); YDInclude('User.php'); $user = new User(); // Let's begin echo "<h1>Let's start working with relations</h1>"; echo "<p>Let's load the \"address\" relation so we can start.</p>"; $user->loadRelation('address'); echo "<p>The loadRelation method includes the necessary files in the relation and instantiates<br>"; echo "the relation objects as parameters of the current object.</p>"; // Let's truncate the table $user->executeSql('TRUNCATE ' . $user->address->getTable()); echo "<p>Let's check if I have my address defined. This relation is a One-to-One relationship. My ID = 1.</p>"; $user->id = 1; // you don't have to pass the relation id if you want to use the same as last time $total = $user->findRelation(); echo "<p>I have " . $total . " addresses defined? That's not true. We have 1 row returned because<br>"; echo "this is a LEFT join. Let's see the results in the address object.</p>"; YDDebugUtil::dump($user->address->getValues()); echo "<p>See? So let's add an address.</p>"; $user->address->user_id = $user->id; $user->address->address = 'Street'; $user->address->city = 'City'; $user->address->state = 'State'; $user->address->country = 'Country'; $user->address->insert(); YDDebugUtil::dump($user->address->getValues()); echo "<p>Done. Now I'll check again if I have any addresses.</p>"; // I reset all info so I'll just check with my ID // The resetRelation method executes a reset at each related object $user->resetRelation();
echo "<h1>We have a Many-to-Many relation</h1>"; echo "<p>This type of relation needs an additional database object (table) to handle the relation.</p>"; echo "<p>When we load this type of relation 2 objects are instantiated. The foreign object and the join object.</p>"; $user->loadRelation('groups'); // Let's truncate the join table $user->executeSql('TRUNCATE ' . $user->join_groups->getTable()); echo "<p>Let's add me to the \"Yellow Duck Framework Group\".</p>"; $user->join_groups->user_id = 1; $user->join_groups->group_id = 1; $user->join_groups->joined = '20040916'; $user->join_groups->active = 1; $user->join_groups->insert(); $user->resetRelation(); echo "<p>Done. Now I can get the results of the relation if I want.</p>"; $user->user_id = 1; $user->findRelation(); YDDebugUtil::dump($user->groups->getValues()); echo "<p>And I have some info of the joining table.</p>"; YDDebugUtil::dump($user->join_groups->getValues()); echo "<p>And my own information, of course.</p>"; YDDebugUtil::dump($user->getValues()); echo "<p>Let's do some more adding.</p>"; // Myself to the second group $user->resetRelation(); $user->join_groups->user_id = 1; $user->join_groups->group_id = 2; $user->join_groups->active = 1; $user->join_groups->insert(); // Pieter to the first group $user->resetRelation(); $user->join_groups->user_id = 2;