public function __construct($singular_name, $pluralize = true, Phactory $phactory) { $this->_singular = $singular_name; if ($pluralize) { $this->_name = Inflector::pluralize($singular_name); } else { $this->_name = $singular_name; } $this->_collection = $phactory->getDb()->selectCollection($this->_name); }
/** * Converts a blueprint array into a persisted object * @param array $blueprint * @return object */ public function create($blueprint) { $name = $blueprint->name; $type = $blueprint->type; if (!isset($this->count[$name])) { $this->count[$name] = 0; } $count = ++$this->count[$name]; $values = $blueprint->values(); $strings = array_map(function ($value) use($count) { return str_replace('#{sn}', str_pad($count, 4, '0', STR_PAD_LEFT), $value); }, $blueprint->strings()); $relationships = $blueprint->relationships(); foreach ($relationships as $key => $relationship) { $relationships[$key] = $relationship->create($blueprint->persisted); } $values = array_merge($values, $strings, $relationships); $dependencies = array_map(function ($value) use($values) { return $value->meet($values); }, $blueprint->dependencies()); $values = array_merge($values, $dependencies); //$object = $this->toObject($name, $values); $object = $this->to_object($name, $values); // @deprecated Backwards compatibility if (false == $blueprint->persisted) { return $object; } \Phactory::triggers()->beforeSave($name, $type, $object); //$object = $this->saveObject($name, $object); $object = $this->save_object($name, $object); // @deprecated Backwards compatibility \Phactory::triggers()->afterSave($name, $type, $object); return $object; }
public function testOverrideAttributes() { $user = Phactory::user(array('last_name' => 'Blarg#{sn}', 'address' => (object) array('street' => 'Sesame St.'))); $this->assertEquals($user, (object) array('first_name' => 'Fronzel', 'last_name' => 'Blarg0001', 'email' => '*****@*****.**', 'address' => (object) array('street' => 'Sesame St.'))); $admin = Phactory::user('admin', array('first_name' => 'Admin')); $this->assertEquals($admin, (object) array('first_name' => 'Admin', 'last_name' => 'Neekburm', 'email' => '*****@*****.**', 'is_admin' => true)); }
public function testToArrayAfterCreate() { $data = array('id' => 1, 'name' => 'testname'); Phactory::define('user', $data); $user = Phactory::create('user'); $this->assertEquals($data, $user->toArray()); }
protected function tearDown() { Phactory::reset(); $this->pdo->exec("DROP TABLE users"); $this->pdo->exec("DROP TABLE images"); $this->pdo->exec("DROP TABLE users_images"); }
protected function setUp() { require_once PHACTORY_PATH . '/Phactory.php'; require_once PHACTORY_PATH . '/Phactory/DbUtil/SqliteUtil.php'; $this->pdo = new PDO('sqlite:test.db'); Phactory::setConnection($this->pdo); }
public function testCustomBuilder() { Phactory::reset(); Phactory::builder(new CustomBuilder()); $cake = Phactory::cake(); $this->assertInstanceOf('CustomObject', $cake); }
public static function getDbUtil(Phactory $phactory) { $db_type = $phactory->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); switch ($db_type) { case 'mysql': return new DbUtil\MysqlUtil($phactory); break; case 'pgsql': return new DbUtil\PgsqlUtil($phactory); break; case 'sqlite': return new DbUtil\SqliteUtil($phactory); break; default: throw new \Exception("DB type '{$db_type}' not found"); break; } }
public function testTriggersWithVariation() { Phactory::reset(); Phactory::triggers(new FrameworkTriggers()); $account = Phactory::account('system'); $this->assertEquals($account->id, 1); $this->assertTrue($account->beforeSave); $this->assertTrue($account->afterSave); $this->assertTrue($account->systemBeforeSave); $this->assertTrue($account->systemAfterSave); }
public static function getDbUtil() { $db_type = Phactory::getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME); switch ($db_type) { case 'mysql': return new Phactory_DbUtil_MysqlUtil(); break; case 'sqlite': return new Phactory_DbUtil_SqliteUtil(); break; default: throw new Exception("DB type '{$db_type}' not found"); break; } }
public function save() { $pdo = Phactory::getConnection(); $sql = "INSERT INTO {$this->_table} ("; $data = array(); $params = array(); foreach ($this->_storage as $key => $value) { $data["`{$key}`"] = ":{$key}"; $params[":{$key}"] = $value; } $keys = array_keys($data); $values = array_values($data); $sql .= join(',', $keys); $sql .= ") VALUES ("; $sql .= join(',', $values); $sql .= ")"; $stmt = $pdo->prepare($sql); $r = $stmt->execute($params); if ($r === false) { $error = $stmt->errorInfo(); Phactory_Logger::error('SQL statement failed: ' . $sql . ' ERROR MESSAGE: ' . $error[2] . ' ERROR CODE: ' . $error[1]); } // only works if table's primary key autoincrements $id = $pdo->lastInsertId(); if ($pk = $this->_table->getPrimaryKey()) { if ($id) { $this->_storage[$pk] = $id; } else { // if key doesn't autoincrement, find last inserted row and set the primary key. $sql = "SELECT * FROM {$this->_table} WHERE"; for ($i = 0, $size = sizeof($keys); $i < $size; ++$i) { $sql .= " {$keys[$i]} = {$values[$i]} AND"; } $sql = substr($sql, 0, -4); $stmt = $pdo->prepare($sql); $stmt->execute($params); $result = $stmt->fetch(PDO::FETCH_ASSOC); $this->_storage[$pk] = $result[$pk]; } } return $r; }
/** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { Phactory::reset(); $this->pdo->exec("DROP TABLE `users`"); }
public function admin() { return array('author' => Phactory::hasOne('author', 'admin')); }
public static function reset() { self::recall(); self::$_blueprints = array(); Phactory_Inflector::reset(); }
public function __construct() { $this->_pdo = Phactory::getConnection(); }
protected function _associateManyToMany($row, $many_to_many) { $pdo = Phactory::getConnection(); foreach ($many_to_many as $name => $arr) { list($to_rows, $assoc) = $arr; foreach ($to_rows as $to_row) { $join_table = $assoc->getJoinTable(); $from_join_column = $assoc->getFromJoinColumn(); $to_join_column = $assoc->getToJoinColumn(); $sql = "INSERT INTO `{$join_table}` \n (`{$from_join_column}`, `{$to_join_column}`)\n VALUES\n (:from_id, :to_id)"; $stmt = $pdo->prepare($sql); $r = $stmt->execute(array(':from_id' => $row->getId(), ':to_id' => $to_row->getId())); if ($r === false) { $error = $stmt->errorInfo(); Phactory_Logger::error('SQL statement failed: ' . $sql . ' ERROR MESSAGE: ' . $error[2] . ' ERROR CODE: ' . $error[1]); } } } }
public function blueprint() { return array('type' => 'jpg', 'content' => '@^&#$^#@&*$', 'creator' => Phactory::hasOne('designer'), 'co_creator' => Phactory::hasOne('designer')); }
public function testRecall() { $name = 'testuser'; // define and create user in db Phactory::define('user', array('name' => $name)); $user = Phactory::create('user'); // recall() deletes from the db Phactory::recall(); // test that the object is gone from the db $stmt = $this->pdo->query("SELECT * FROM `users`"); $db_user = $stmt->fetch(); $this->assertFalse($db_user); // test that the blueprints weren't destroyed too $user = Phactory::create('user'); $this->assertEquals($user->name, $name); }
public function tearDown() { Phactory::recall(); }
public function blueprint() { return array('name' => 'Ore #{sn}', 'mine' => Phactory::hasOne('mine'), 'saved' => false); }
/** * Get triggers events caller. If it is not defined, it will be set. * @param object|null $triggers * @return \Phactory\Triggers */ public static function triggers($triggers = null) { if (is_object($triggers)) { self::$triggers = new Triggers($triggers); } return isset(self::$triggers) ? self::$triggers : (self::$triggers = new Triggers()); }
public function blueprint() { return array('title' => 'project #{sn}', 'category' => Phactory::hasOne('category', 'tshirt')); }
public function admin() { return array('employer' => Phactory::has_one('employer', 'admin')); }
public function testRecall() { $name = 'testuser'; // define and create user in db Phactory::define('user', array('name' => $name)); $user = Phactory::create('user'); // recall() deletes from the db Phactory::recall(); // test that the object is gone from the db $db_user = $this->db->users->findOne(); $this->assertNull($db_user); // test that the blueprints weren't destroyed too $user = Phactory::create('user'); $this->assertEquals($user['name'], $name); }