示例#1
0
 public function actionCreate()
 {
     //$data = (array)json_decode( file_get_contents('php://input') );
     //echo (var_dump($data));
     $model = new Post();
     $model->setAttributes($this->getJsonInput());
     //$model->setAttributes($data);
     if (!$model->validate()) {
         $this->sendResponse(400, CHtml::errorSummary($model));
     } else {
         if (!$model->save(false)) {
             throw new CException('Cannot create a record');
         }
     }
     $model->refresh();
     echo CJSON::encode($model);
     /*
     		if(isset($_GET['id']))
                 $id=$_GET['id'];
     		$posts = Post::model()->findAllByAttributes(array("author"=>1));
     		foreach($posts as $p) {
     			$author = User::model()->findByPk($p['author']);
     			$p['author'] = $author['name'];
     		}
     		echo CJSON::encode($posts);*/
 }
示例#2
0
 public function testModel()
 {
     $model = Post::model();
     $this->assertTrue($model instanceof Post);
     $this->assertTrue($model->dbConnection === $this->_connection);
     $this->assertTrue($model->dbConnection->active);
     $this->assertEquals('posts', $model->tableName());
     $this->assertEquals('id', $model->tableSchema->primaryKey);
     $this->assertTrue($model->tableSchema->sequenceName === '');
     $this->assertEquals(array(), $model->attributeLabels());
     $this->assertEquals('Id', $model->getAttributeLabel('id'));
     $this->assertEquals('Author Id', $model->getAttributeLabel('author_id'));
     $this->assertTrue($model->getActiveRelation('author') instanceof CBelongsToRelation);
     $this->assertTrue($model->tableSchema instanceof CDbTableSchema);
     $this->assertTrue($model->commandBuilder instanceof CDbCommandBuilder);
     $this->assertTrue($model->hasAttribute('id'));
     $this->assertFalse($model->hasAttribute('comments'));
     $this->assertFalse($model->hasAttribute('foo'));
     $this->assertEquals(array('id' => null, 'title' => null, 'create_time' => null, 'author_id' => null, 'content' => null), $model->attributes);
     $post = new Post();
     $this->assertNull($post->id);
     $this->assertNull($post->title);
     $post->setAttributes(array('id' => 3, 'title' => 'test title'));
     $this->assertNull($post->id);
     $this->assertEquals('test title', $post->title);
 }
 /**
  * Tests correct date/time setting.
  *
  * @return void
  * @since 0.1.0
  */
 public function testBehavior()
 {
     // mocking failed :(
     /*$post = Stub::Make(
           'Post',
           array(
               'insert' => function () {
                   return true;
               },
               'update' => function () {
                   return true;
               },
               'name' => 'Dummy name',
               'content' => 'Dummiest content',
               'user_id' => 1,
               'category_id' => 1,
           )
       );*/
     $post = new \Post();
     $post->setAttributes(array('name' => 'Dummy name', 'content' => 'Dummiest content', 'user_id' => 1, 'category_id' => 1), false);
     $this->assertNull($post->created);
     $post->save();
     $dt = new \DateTime();
     $this->assertSame($dt->format(\DateTime::ISO8601), $post->created);
     usleep(1.1 * 1000 * 1000);
     $post->save();
     $this->assertSame($dt->format(\DateTime::ISO8601), $post->created);
 }
 public function add($attr, $image)
 {
     $model = new Post();
     $model->setAttributes($attr);
     $model->date = time();
     $model->post_comment_count = 0;
     $model->post_like_count = 0;
     $image_url = null;
     if (isset($image)) {
         $image_url = UploadHelper::getUrlUploadSingleImage($image, $attr['user_id']);
     }
     $location = new Location();
     $location->longitude = $attr['lng'];
     $location->latitude = $attr['lat'];
     $location->name = $attr['name'];
     $location->save(FALSE);
     $model->image = $image_url;
     $model->location_id = $location->location_id;
     if ($model->save(FALSE)) {
         $subject_arr = json_decode($attr['subject'], true);
         foreach ($subject_arr as $item) {
             $subject_post = new PostSubject();
             $subject_post->post_id = $model->post_id;
             $subject_post->subject_id = $item;
             $subject_post->save(FALSE);
         }
         return TRUE;
     }
     return FALSE;
 }
示例#5
0
 /**
  * Tests SlugBehavior validation.
  *
  * @return void
  * @since 0.1.0
  */
 public function testValidation()
 {
     $defaultAttributes = array('name' => 'Test post post post', 'content' => 'Long enough to conform validation', 'user_id' => 1, 'category_id' => 1, 'slug' => 'admin');
     $post = new \Post();
     $post->setAttributes($defaultAttributes, false);
     if ($post->save(false)) {
         $message = 'Restricted slug hasn\'t been detected by SlugBehavior ' . '(resulting slug: [' . $post->slug . '])';
         $this->fail($message);
     }
     $this->assertTrue(in_array('slugBehavior.restrictedSlug', $post->getErrors('slug'), true));
     $post->setAttributes(array('slug' => '', 'name' => ''), false);
     $post->validate(array());
     if ($post->save(false)) {
         $this->fail('Empty slug hasn\'t been detected by SlugBehavior');
     }
     $this->assertTrue(in_array('slugBehavior.emptySlug', $post->getErrors('slug'), true));
 }
示例#6
0
 public function test_should_update_datetime_correctly()
 {
     $params = array('title' => 'Expiring salutation', 'body' => 'Expiring Hello world!', 'expires_at(1i)' => '2007', 'expires_at(2i)' => '10', 'expires_at(3i)' => '15', 'expires_at(4i)' => '17', 'expires_at(5i)' => '30');
     $Post = new Post();
     $Post->setAttributes($params);
     $this->assertTrue($Post->save());
     $Post->reload();
     $this->assertEqual($Post->get('expires_at'), '2007-10-15 17:30:00');
 }
示例#7
0
 /**
  * Verifies that main cache key resets every time post is edited.
  *
  * @return void
  * @since 0.1.0
  */
 public function testCacheInvalidation()
 {
     $value = \Yii::app()->cacheHelper->getPostsCacheTokenValue();
     $post = new \Post();
     $attrs = array('name' => 'just a name', 'slug' => 'just-a-name', 'content' => 'Dummy content', 'created' => '2010-05-05 12:12:12', 'user_id' => 1, 'category_id' => 1);
     $post->setAttributes($attrs, false);
     $post->save();
     $this->assertNotSame($value, $value = \Yii::app()->cacheHelper->getPostsCacheTokenValue());
     $post->delete();
     $this->assertNotSame($value, $value = \Yii::app()->cacheHelper->getPostsCacheTokenValue());
 }
 /**
  * Creates a new post.
  * If creation is successful, the browser will be redirected to the 'show' page.
  */
 public function actionCreate()
 {
     $post = new Post();
     if (Yii::app()->request->isPostRequest) {
         if (isset($_POST['Post'])) {
             $post->setAttributes($_POST['Post']);
         }
         if ($post->save()) {
             $this->redirect(array('show', 'id' => $post->id));
         }
     }
     $this->render('create', array('post' => $post));
 }
示例#9
0
 public function actionCreate()
 {
     $model = new Post();
     $model->setAttributes($this->getJsonInput());
     if (!$model->validate()) {
         $this->sendResponse(400, CHtml::errorSummary($model));
     } else {
         if (!$model->save(false)) {
             throw new CException('Cannot create a record');
         }
     }
     $model->refresh();
     $this->sendResponse(200, CJSON::encode($model));
 }
示例#10
0
 /**
  * Управление записями.
  *
  * @return void
  */
 public function actionIndex()
 {
     $model = new Post('search');
     $model->unsetAttributes();
     // clear any default values
     $model->comment_status = true;
     if (Yii::app()->getRequest()->getParam('Post')) {
         $model->setAttributes(Yii::app()->getRequest()->getParam('Post'));
     }
     $this->render('index', array('model' => $model));
 }
 /**
  * Управление записями.
  *
  * @return void
  */
 public function actionIndex()
 {
     $model = new Post('search');
     $model->unsetAttributes();
     // clear any default values
     if (Yii::app()->getRequest()->getParam('Post')) {
         $model->setAttributes(Yii::app()->getRequest()->getParam('Post'));
     }
     $this->render('index', ['model' => $model]);
 }