public function testGetData()
 {
     $record = $this->authitemchilds('authitemchild_1');
     $admin = $this->authitems('admin');
     $_GET['with'] = 'parent, child';
     $relationProvider = new ApiRelationProvider(array("config" => array('parent' => array('relationName' => 'parent0', 'columnName' => 'parentColumn'), 'child' => array('relationName' => 'child0', 'return' => 'object'))));
     $relationData = $relationProvider->getData($record);
     $this->assertTrue(is_array($relationData));
     $this->assertArrayHasKey('parentColumn', $relationData);
     $this->assertArrayHasKey('name', $relationData['parentColumn']);
     $this->assertArrayHasKey('description', $relationData['parentColumn']);
     $this->assertArrayHasKey('bizrule', $relationData['parentColumn']);
     $this->assertArrayHasKey('child', $relationData);
     $this->assertInstanceOf('CActiveRecord', $relationData['child']);
     $_GET['with'] = 'chidrens';
     $relationProvider = new ApiRelationProvider(array("config" => array('chidrens' => array('relationName' => 'authItemChildren', 'columnName' => 'chidrens', 'return' => 'array'))));
     $relationData = $relationProvider->getData($admin);
     $this->assertTrue(is_array($relationData));
     $this->assertArrayHasKey('chidrens', $relationData);
     $this->assertEquals(count($relationData['chidrens']), count($admin->authItemChildren));
 }
Ejemplo n.º 2
0
 /**
  * Function send response with list of record attributes or empty array if no record found.
  * 
  * @param boolean $sendToEndUser Whether the response should be sent to the end user or should be returned as an array.
  * @return array result. Null if the result sended to end user.
  */
 public function getList($sendToEndUser = null)
 {
     if (is_bool($sendToEndUser)) {
         $this->sendToEndUser = $sendToEndUser;
     }
     $this->checkModel();
     $this->criteria = new CDbCriteria($this->getFinalCriteriaParams());
     try {
         $relationData = new ApiRelationProvider(array('config' => $this->getRelations(), 'model' => $this->model));
     } catch (CException $e) {
         $result = array('error' => array($e->getMessage()));
         $this->statusCode = 400;
         return $this->returnResult($result);
     }
     $this->criteria->with = $relationData->getRelationsList();
     if (is_array($this->criteria->with) && !empty($this->criteria->with)) {
         $this->criteria->together = true;
     }
     $this->criteria->mergeWith($this->getFilterCriteria(), 'OR');
     $this->criteria->mergeWith($this->getSearchCriteria(), 'OR');
     $this->criteria->mergeWith($this->baseCriteria, 'AND');
     try {
         $records = $this->model->findAll($this->criteria);
         $result = array();
         foreach ($records as $record) {
             $result[] = array_merge($record->attributes, $relationData->getData($record));
         }
     } catch (Exception $ex) {
         $message = property_exists($ex, 'errorInfo') ? $ex->errorInfo : $ex->getMessage();
         $result = array("error" => $message);
         $this->statusCode = 400;
     }
     return $this->returnResult($result, $this->statusCode == 200 ? array($this->getContentRangeHeader()) : array());
 }