<?php //We admit that Developers & Languages classes and their tables exist //Get the developer with id 1 $myDeveloper = Developers::getById(1); //Check if developer is found if (!empty($myDeveloper)) { //Get language with ID 3 $myLanguage = Languages::getById(3); if (!empty($myLanguage)) { //Load the "languages" properties of the developer $languages = $myDeveloper->load('languages'); //Add the new language to this list $languages->push($myLanguage); //And save the changes! Developers::update($myDeveloper); } else { echo 'The new language used by the developer is not found.'; } } else { echo 'The developer to update is not found.'; }
<?php require "paths.php"; require "vendor/autoload.php"; require "app/database.php"; error_reporting(E_ALL); ini_set('display_errors', 'on'); set_time_limit(0); require "install/migrations/0-organizations.php"; require "install/migrations/1-developers.php"; $OrganizationsMigration = new Organizations(); $OrganizationsMigration->up(); echo "migrated organizations\n"; $DevelopersMigration = new Developers(); $DevelopersMigration->up(); echo "migrated developers\n";
class Developers extends \EntityPHP\Entity { protected $firstname; protected $lastname; protected $languages; //__structure() method is mandatory and must return an array public static function __structure() { return array('firstname' => 'VARCHAR(255)', 'lastname' => 'VARCHAR(255)', 'languages' => array('Languages')); } } //Init connection to the database \EntityPHP\Core::connectToDB('localhost', 'entityphp', '3n7i7iPHP', 'entityphp'); //Generate the database (this method should be execute only once) \EntityPHP\Core::generateDatabase(); //Create several languages $php = new Languages('PHP'); $javascript = new Languages('JavaScript'); $c = new Languages('C'); $ruby = new Languages('Ruby'); $python = new Languages('Python'); //Store all languages in one call thanks to addMultiple() Languages::addMultiple(array($php, $javascript, $c, $ruby, $python)); //Create several developers $jean = new Developers(array('firstname' => 'Jean', 'lastname' => 'Peplu', 'languages' => array($php, $javascript))); $mona = new Developers(array('firstname' => 'Mona', 'lastname' => 'Bruti', 'languages' => array($ruby, $python))); $theo = new Developers(array('firstname' => 'Théo', 'lastname' => 'Bligé', 'languages' => array($c))); $homer = new Developers(array('firstname' => 'Homer', 'lastname' => 'Dalor', 'languages' => array())); //Store all developers in one call thanks to addMultiple() Developers::addMultiple(array($jean, $mona, $theo, $homer));