/** * Add values to the insert * * @param array $values * @return void */ public function values(array $values) { // do nothing if we get nothing if (empty($values)) { return $this; } // check if the the passed array is a collection. // because we want to be able to insert bulk values. if (!\CCArr::is_collection($values)) { $values = array($values); } // because we could recive the arrays in diffrent order // we have to sort them by their key. foreach ($values as $key => $value) { ksort($value); $values[$key] = $value; } // merge the new values with the existing ones. $this->values = array_merge($this->values, $values); // return self so we can continue running the next function return $this; }
/** * Assign an model with some data * * @param array $data * @return CCModel */ public static function assign($data) { // return null if we have no data if ($data === null || empty($data)) { return null; } // do we have a collection or a single result? if (CCArr::is_collection($data)) { $return = array(); foreach ($data as $key => $item) { $return[$key] = static::create($item); } return $return; } return static::create($data); }
/** * test if array contains other arrays */ public function testArrayIsCollection() { $this->assertTrue(CCArr::is_collection(array(array('name' => 'johnson', 'age' => 20)))); $this->assertTrue(CCArr::is_collection(array(array('name' => 'johnson', 'age' => 20), array('name' => 'Jack', 'age' => 25)))); $this->assertFalse(CCArr::is_collection(array('no array valie', array('name' => 'johnson', 'age' => 20), array('name' => 'Jack', 'age' => 25)))); $this->assertFalse(CCArr::is_collection(array('jack', 'john', 'johnson'))); $this->assertFalse(CCArr::is_collection(array('jack' => 12, 'john' => 24, 'johnson' => 32))); }
/** * Model finder * This function allows you direct access to your records. * * @param mixed $param * @param mixed $param2 * @return CCModel */ public static function find($param = null, $param2 = null) { $settings = static::_model(); $query = DB::select($settings['table']); // Do we have a find modifier? if (!is_null($settings['find_modifier'])) { $callbacks = $settings['find_modifier']; if (!\CCArr::is_collection($callbacks)) { $callbacks = array($callbacks); } foreach ($callbacks as $call) { if (is_callable($call)) { call_user_func_array($call, array(&$query)); } else { throw new ModelException("Invalid Callback given to find modifiers."); } } } if (!is_null($param)) { // Check if paramert 1 is a valid callback and not a string. // Strings as function callback are not possible because // the user might want to search by key like: // Model::find( 'key', 'type' ); if (is_callable($param) && !is_string($param)) { call_user_func_array($param, array(&$query)); } elseif (is_null($param2)) { $query->where($settings['table'] . '.' . $settings['primary_key'], $param)->limit(1); } elseif (!is_null($param2)) { $query->where($param, $param2)->limit(1); } } // alway group the result $query->forward_key($settings['primary_key']); // and we have to fetch assoc $query->fetch_arguments = array('assoc'); // and assign return static::assign($query->run()); }