コード例 #1
0
ファイル: Nested.php プロジェクト: snowfire/database
 public function create(array $fields, $foreign_models = array())
 {
     if (isset($fields['next_to_node'])) {
         $transaction = true;
         $fields['left'] = $fields['next_to_node']['right'] + 1;
         $fields['right'] = $fields['left'] + 1;
         $this->_transaction_begin();
         $this->_query_update()->set('`left` = `left` + 2')->where('`left` >', $fields['next_to_node']['left'])->execute();
         $this->_query_update()->set('`right` = `right` + 2')->where('`right` >', $fields['next_to_node']['right'])->execute();
     } else {
         if (isset($fields['inside_node'])) {
             $transaction = true;
             $fields['left'] = $fields['inside_node']['left'] + 1;
             $fields['right'] = $fields['left'] + 1;
             $this->_transaction_begin();
             $this->_query_update()->set('`left` = `left` + 2')->where('`left` >', $fields['inside_node']['left'])->execute();
             $this->_query_update()->set('`right` = `right` + 2')->where('`right` >', $fields['inside_node']['left'])->execute();
         } else {
             if (!isset($fields['left'], $fields['right'])) {
                 $transaction = true;
                 $this->_transaction_begin();
                 $last_right = $this->_query_select('MAX(`right`) AS max_right')->limit(1)->execute(array('single_column' => true));
                 $fields['left'] = $last_right + 1;
                 $fields['right'] = $last_right + 2;
             }
         }
     }
     unset($fields['next_to_node'], $fields['inside_node']);
     parent::create($fields, $foreign_models);
     if ($transaction) {
         $this->_transaction_end();
     }
 }
コード例 #2
0
 public function testDelete()
 {
     $this->_mock = new Mock_Database($this, array(array('many', "SELECT *\nFROM people\nWHERE `id` IN (?, ?)", array(1, 2), null, array('return' => array(array('id' => '1', 'passport_id' => 3), array('id' => '2', 'passport_id' => 4)))), array('execute', "DELETE FROM people\nWHERE `id` IN (?, ?)", array(1, 2)), array('many', "SELECT *\nFROM passports\nWHERE `id` IN (?, ?)", array(3, 4), null, array('return' => array(array('id' => '3'), array('id' => '4')))), array('execute', "DELETE FROM passports\nWHERE `id` IN (?, ?)", array(3, 4))), array('debug' => false));
     \Snowfire\Database\Model::database($this->_mock);
     $people_model = new People();
     $passports_model = new Passports();
     $people_model->delete(array('id' => array(1, 2)), array('passports' => $passports_model));
 }
コード例 #3
0
 public function testDelete()
 {
     $this->_mock = new Mock_Database($this, array(array('many', "SELECT *\nFROM products\nWHERE `id` = ?", array(1), null, array('return' => array(array('id' => '1')))), array('execute', "DELETE FROM products_categories\nWHERE `product_id` IN (?)", array(1)), array('execute', "DELETE FROM products\nWHERE `id` = ?", array(1))), array('debug' => false));
     \Snowfire\Database\Model::database($this->_mock);
     $prod_model = new Products1();
     $cat_model = new Categories1();
     $prod_model->delete(array('id' => 1), array('categories' => $cat_model));
 }
コード例 #4
0
 public function testMany()
 {
     $this->_mock = new Mock_Database($this, array(array('many', "SELECT products.*\nFROM products", array(), null, array('return' => array(array('id' => 1), array('id' => 2)))), array('many', "SELECT products_options.*\nFROM products_options\nWHERE `product_id` IN (?, ?)", array(1, 2), null, array('return' => array(array('id' => 3, 'product_id' => 1), array('id' => 4, 'product_id' => 2))))));
     \Snowfire\Database\Model::database($this->_mock);
     $prod_model = new Products();
     $opt_model = new Products_Options();
     $this->assertEquals(array(array('id' => 1, 'options' => array(array('id' => 3, 'product_id' => 1))), array('id' => 2, 'options' => array(array('id' => 4, 'product_id' => 2)))), $prod_model->many(array('foreign_models' => array('options' => $opt_model))));
 }
コード例 #5
0
ファイル: ModelTest.php プロジェクト: snowfire/database
 private function _users_model($expected)
 {
     $this->_mock = new Mock_Database($this, $expected);
     \Snowfire\Database\Model::database($this->_mock);
     return new Users(array('account_id' => 5));
 }
コード例 #6
0
ファイル: Model.php プロジェクト: snowfire/database
 private function _get_many_to_many_post($single_row, $rows, self $foreign_model)
 {
     $foreign_singular = $foreign_model->singular();
     $col = $foreign_singular . '_ids';
     $foreign_plural = $foreign_model->plural();
     $foreign_ids = array();
     $index = array();
     if ($single_row) {
         $foreign_ids = $rows[$col] ? explode(',', $rows[$col]) : array();
         $rows[$foreign_plural] = array();
         unset($rows[$col]);
     } else {
         foreach ($rows as &$row) {
             if ($row[$col]) {
                 foreach (explode(',', $row[$col]) as $id) {
                     $foreign_ids[] = $id;
                     $index[$id][] =& $row;
                 }
             }
             $row[$foreign_plural] = array();
             unset($row[$col], $row);
         }
     }
     $foreign_rows = $foreign_model->many(array('conditions' => array('id' => $foreign_ids)));
     if ($single_row) {
         $rows[$foreign_plural] = $foreign_rows;
     } else {
         foreach ($foreign_rows as $row) {
             foreach ($index[$row['id']] as &$r) {
                 $r[$foreign_plural][] = $row;
                 unset($r);
             }
         }
     }
     return $rows;
 }