Create and Update of Posts not Saving in Blog Tutorial

I have been following the blog tutorial through Post Management - Creating and Updating Posts. Everything seems to be working, but my creates and updates are not being saved. I’ve tried with both MySQL and SQLite. I get no error message and my dropdown list for status is populated with choices, so I know that I am communicating with the database (with either MySQL or SQLite).

If I run the Blog Demo program I can create and edit posts. My Blog Tutorial code looks essentially the same as the Blog Demo code. I’ve even copied over the string2array, array2string, addTags and removeTags functions.

If I make changes in the accessRules() function in PostController.php, they have the expected result. For example:


	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'users'=>array('@'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

Allows the account demo|demo to create and manage posts. However,




	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

allows demo|demo to create post but not to manage posts.

However neither version of accessRules() results in any created or edited posts being saved.

Any thoughts or help would be appreciated.

So you’re able to create and edit posts, except that they don’t get saved in the db? I also think the Blog tutorial isn’t updated, a colleague ran into some errors if you follow the tutorial word for word.

Also, try to check the application logs in /protected/runtime. There might be some clues in there.

Thanks for your reply. I’ve added trace and info to my logging along with CWebLogRoute and CProfileLogRoute.

In the log file I see some statements where the create and update pages are loaded and displayed, but I am not seeing entries that occur when I click on the Create or Save button. Perhaps I don’t have the correct logging set up. The messages I see are mostly of the form:

Which is populating the page but not the action of the post. Sorry, I am very new to Yii, so perhaps you can tell me where to insert some more specific tracing?

Incidentally, I was able to follow Larry Ullman’s tutorial using MySQL and I can create employee records. This problem is only occurring with the Blog Tutorial.

Again, thank you for your assistance

Are these logs shown on the page? Or on a log file?

My log settings look like this:


'log' => array(

  'class'=>'CLogRouter',

  'routes'=>array(

    array(

      'class'=>'CFileLogRoute',

      'levels'=>'error, warning',

    ),    

  ),

),

Whenever there are errors, it will put them in /protected/runtime/application.log.

Both to file and screen. I have three routes set up CFileLogRoute, CWebLogRoute and CProfileLogRoute. I have error, warning, trace and info set too.

However its not showing what happens after I click the button. Only before. So I either need to set tracing on someplace else or I need to learn more about where in Yii the actual communication is taking place.

Perhaps I should just start over and try to create the blog again??

Thanks,

Don

You could do it the old way. I’m using this as reference.

Replace actionCreate with:


/**

 * Creates a new model.

 * If creation is successful, the browser will be redirected to the 'view' page.

 */

public function actionCreate()

{

  $model=new Post;

  var_dump($_POST['Post']); // see if we're getting data from $_POST

  if(isset($_POST['Post']))

  {

    $model->attributes=$_POST['Post'];

    $result = $model->save();

    var_dump($result); // check if save was successful

    if (!$result)

      var_dump($result->getErrors()); // show errors if save as not successful

    die(); // exit so we'll see our debug messages

    if ($result);

      $this->redirect(array('view','id'=>$model->id));

  }

  

  $this->render('create',array(

    'model'=>$model,

  ));

}

Shiki,

Once Again, thanks for your reply. I tried your suggested actionCreate and var_dump($result)=bool(false).

Unfortunately, getErrors() is failing too with "Fatal error: Call to a member function getErrors() on a non-object in C:\xampp\htdocs\yii\demos\blog2\protected\controllers\PostController.php on line 72", so I cannot get additional info.

I put some var_dump statements in Yii/framework/db/ar/CActiveRecord.php to see what was happening inside of the base save() function. I var_dump($attributes) in save() and get a NULL result with the Blog Demo and Blog Tutorial. So, it appears to be behaving similarly even though this base class works fine in the blog demo and it is not working on the blog tutorial.

I tried using $result = $model->save(0,$model->attributes); in PostController.php in my Blog Tutorial to force the data and I end up with a record inserted into the database with all NULL values. So at least that caused an insert to occur.

But so far I am not sure why this is failing.

On the plus side, I am getting familiar with the framework and its inner workings and hierarchy. :slight_smile: So I thank you very much for that.

As mentioned before. I think something went wrong somewhere earlier in the creation of this demo and its best if I simply start over. I will post here if that fixes the problem or if I encounter this blocking error again.

Thank you.

Donald

Woah sorry! It should have been:


if (!$result)

      var_dump($model->getErrors()); // show errors if save as not successful

Sorry for messing that up!