示例#1
0
 public function setUp()
 {
     Connection::instance()->execute('truncate table users');
     Connection::instance()->execute('truncate table countries');
     User::$has_one = array(array('friend', 'class' => 'User', 'foreign_key' => 'friend_id'));
     Country::$has_many = array(array('users'));
 }
示例#2
0
 public function testPrepare()
 {
     $connection = Connection::instance();
     $this->assertEquals(array('sql' => 'select * from users where age > 10', 'values' => array()), Reflection::invokeMethod($connection, '_prepare', array('select * from users where age > 10', array())));
     $this->assertEquals(array('sql' => 'select * from users where age > ?', 'values' => array(10)), Reflection::invokeMethod($connection, '_prepare', array('select * from users where age > ?', array(10))));
     $this->assertEquals(array('sql' => 'select * from users where id in (?,?) or (country = ? and city not in (?,?,?))', 'values' => array(1, 2, 'China', 'ShangHai', 'GuangZhou', 'BeiJing')), Reflection::invokeMethod($connection, '_prepare', array('select * from users where id in (?) or (country = ? and city not in (?))', array(array(1, 2), 'China', array('ShangHai', 'GuangZhou', 'BeiJing')))));
 }
示例#3
0
 public function __call($method, $arguments)
 {
     if (!in_array($method, array('create', 'update', 'destroy'))) {
         throw new UndefinedMethodException($method, get_class($this));
     }
     $closure = $arguments[0];
     $relationshipsCount = count($this->_hasMany->getMetadata()) + count($this->_hasOne->getMetadata()) + count($this->_belongsTo->getMetadata());
     if ($relationshipsCount <= 0) {
         return $closure();
     }
     return Connection::instance()->transaction(function () use($method, $closure) {
         if ($this->_belongsTo->{$method}() === false) {
             return false;
         }
         if ($method !== 'destroy') {
             if ($closure() === false) {
                 return false;
             }
         }
         if ($this->_hasMany->{$method}() === false) {
             return false;
         }
         if ($this->_hasOne->{$method}() === false) {
             return false;
         }
         if ($method === 'destroy') {
             if ($closure() === false) {
                 return false;
             }
         }
         return true;
     });
 }
示例#4
0
 public function testCreate()
 {
     $this->assertTrue(self::$_table->create('users', function ($table) {
         $table->integer('id', array('auto_increment' => true));
         $table->integer('age', array('unsigned' => true));
         $table->string('name', array('length' => 40));
         $table->timestamps();
         $table->index('name');
     }));
     $sql = '
         CREATE TABLE `users` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `age` int(11) unsigned DEFAULT NULL,
             `name` varchar(40) DEFAULT NULL,
             `created_at` datetime DEFAULT NULL,
             `updated_at` datetime DEFAULT NULL,
             PRIMARY KEY (`id`),
             KEY `index_name` (`name`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8
     ';
     $this->assertEquals(preg_replace('/\\s/', '', $sql), preg_replace('/\\s/', '', Connection::instance()->fetch('show create table users')[0]['Create Table']));
 }
示例#5
0
 public static function destroyAll($conditions = array())
 {
     $params = SqlBuilder::parseDestroySql(static::table(), $conditions);
     return Connection::instance()->execute($params['sql'], $params['values']);
 }