Yii Framework Forum: [SOLVED] How do I include a partial from another folder, with no modules? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[SOLVED] How do I include a partial from another folder, with no modules? Rate Topic: -----

#1 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 22 February 2012 - 10:19 PM

How do I include a partial from another folder, with no modules?

I had the default Yii setup with the default folders, and then I...
  • Created a Users model for the users database
  • inside protected/views/users there is a form called _form.php
  • I have made a new page inside protected/views/site/pages called apply.php
  • I want to include the _form.php from users, inside another folder (views/site/pages).


How do I do it?

<?php echo $this->renderPartial('SiteController/_form'); ?>

does not work
0

#2 User is offline   rookie84 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 16-November 11

Posted 22 February 2012 - 10:46 PM

Here's how you can do it:

$this->renderPartial('//users/_form'); //In case _form.php is inside views/users/

Using the // double slashes you can access the root of the views

View Postdesbest, on 22 February 2012 - 10:19 PM, said:

How do I include a partial from another folder, with no modules?

I had the default Yii setup with the default folders, and then I...
  • Created a Users model for the users database
  • inside protected/views/users there is a form called _form.php
  • I have made a new page inside protected/views/site/pages called apply.php
  • I want to include the _form.php from users, inside another folder (views/site/pages).


How do I do it?

<?php echo $this->renderPartial('SiteController/_form'); ?>

does not work

0

#3 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 23 February 2012 - 11:03 AM

View Postrookie84, on 22 February 2012 - 10:46 PM, said:

Here's how you can do it:

$this->renderPartial('//users/_form'); //In case _form.php is inside views/users/

Using the // double slashes you can access the root of the views


Thanks! It works!
0

#4 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 23 February 2012 - 03:29 PM

Now I've got a new problem.
When I include the form, I get an error that says

Quote

The "dataProvider" property cannot be empty.


The code causing this is protected/views/site/pages/apply.php
<h1>Users</h1>

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
)); ?>


However, the dataProvider variable is defined in SiteController, (I think). So why isn't it recognised?


protected/controllers/SiteController.php
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Users');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

0

#5 User is offline   rookie84 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 16-November 11

Posted 24 February 2012 - 01:52 AM

As it says $dataProvider is not defined because you have it defined in the actionIndex and you are rendering 'index' instead of apply.

This should fix it: (I am not sure if you are rendering it partially or what but this is how it should be done)

	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Users');
		$this->render('pages/apply',array(
			'dataProvider'=>$dataProvider,
		));
	}



View Postdesbest, on 23 February 2012 - 03:29 PM, said:

Now I've got a new problem.
When I include the form, I get an error that says


The code causing this is protected/views/site/pages/apply.php
<h1>Users</h1>

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
)); ?>


However, the dataProvider variable is defined in SiteController, (I think). So why isn't it recognised?


protected/controllers/SiteController.php
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Users');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


0

#6 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 10:48 AM

That would work if I was rendering the page directly, but I'm not.
Using the default Yii project, I'm actually using the site/pages views, where I can make new pages by making .php files in that folder, and not have to make a new action for them.

The code for the Controller to view the site/page?view=apply page, goes like this.

<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Users');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


So where am I meant to place the $dataProvider variable? In the CViewAction class inside the actual Yii folder? Inside the actions() function (which doesn't work)? I'm stuck.

View Postrookie84, on 24 February 2012 - 01:52 AM, said:

As it says $dataProvider is not defined because you have it defined in the actionIndex and you are rendering 'index' instead of apply.

This should fix it: (I am not sure if you are rendering it partially or what but this is how it should be done)

	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Users');
		$this->render('pages/apply',array(
			'dataProvider'=>$dataProvider,
		));
	}


0

#7 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 24 February 2012 - 10:56 AM

Quote

I'm actually using the site/pages views,


I think this feature was really only intended for the occasional static page.

Just create a new action and things will start to make sense.

public function actionApply()
{
// capture $_GET parameters (or $_POST if using a form)

 // define your model
 
// display a view etc

}



doodle
Check out myYii powered website
0

#8 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 12:01 PM

This would work, but when I use renderPartial() to pass the $dataProvider variable to the partial that is being included, I get a fatal error that says that the object cannot be converted to a string.

Quote

Object of class CActiveDataProvider could not be converted to string


<?php echo $this->renderPartial('//users/index', "$dataProvider"); ?>


View Postgot 2 doodle, on 24 February 2012 - 10:56 AM, said:

I think this feature was really only intended for the occasional static page.

Just create a new action and things will start to make sense.

public function actionApply()
{
// capture $_GET parameters (or $_POST if using a form)

 // define your model
 
// display a view etc

}



doodle

0

#9 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 24 February 2012 - 12:22 PM

If you are rendering a partial view then you have to pass the object to the partial view, just like you did with render.
<?php echo $this->renderPartial('SiteController/_form',array('var1'=>$var1,'var2'=>$var2)); ?>


I use this often when formatting links etc.
foreach ($categories  as $category) {
    $this->renderPartial('_requirementItem',array(
        'category'=>$category,
        'services'=>$services,
        'woodlot'=>$model
    ));
}

Then in the partial view
<?php echo $category->name ?>


Note also that the partial view can also render partial views, so you can pass a collection to a partial view and that view can loop through the collection but it has to pass the data to each view if that view is referencing it.

I have also adopted the naming standard where partial views start with an underscore, it helps me to keep organized.



does that help?

doodle
Check out myYii powered website
0

#10 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 01:28 PM

Yes it does help.

But now when I submit the form, I receive this error.

Quote

Fatal error: Call to undefined method stdClass::save() in E:\mowes\www\vapacloud\protected\controllers\SiteController.php on line 39

	public function actionCreate()
	{
		$model=new Users;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}


I have no idea why this error is happening, as the model is correctly defined, and I copied the code from the Users controller, so it should work.
0

#11 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 24 February 2012 - 01:34 PM

What does your model look like?

Does it extend CActiveRecord?
class CatalogItem extends CActiveRecord


doodle
Check out myYii powered website
0

#12 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 02:57 PM

I've managed to fix that, so that the form can POST correctly.
But it says that I keep leaving a field blank, even when it isn't blank.
I can't see anywhere in my code that would cause the dateofbirth form field not to be recognised.

PS. dateofbirth is a VARCHAR field in mysql, but it is a date input in the form. I don't think that should be a problem.

	<div class="row">
		<?php echo $form->labelEx($model,'dateofbirth'); ?>
		<?php
		$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'dateofbirth',
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
    ));
    ?>


  public function actionApply()
  {
		$model=new Users;
		
		// $this->performAjaxValidation($model);
		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			if($model->save())
				$this->redirect('site/apply');
		}

    $dataProvider=new CActiveDataProvider('Users');

    $this->render('apply',array(
			'dataProvider'=>$dataProvider,
			'model'=>$model,
		));
  }


<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property integer $level
 * @property string $firstname
 * @property string $lastname
 * @property string $firstline
 * @property string $secondline
 * @property string $city
 * @property string $postcode
 * @property string $phonenumber
 * @property string $dateofbirth
 * @property integer $upline
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth', 'required'),
			array('level, upline', 'numerical', 'integerOnly'=>true),
			array('username, password, email, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, email, level, firstname, lastname, firstline, secondline, city, postcode, phonenumber, dateofbirth, upline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'level' => 'Level',
			'firstname' => 'First Name',
			'lastname' => 'Last Name',
			'firstline' => 'Road name and number',
			'secondline' => '2nd line of address',
			'city' => 'City',
			'postcode' => 'Postcode',
			'phonenumber' => 'Phone number',
			'dateofbirth' => 'Date of birth',
			'upline' => 'Upline',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('level',$this->level);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('firstline',$this->firstline,true);
		$criteria->compare('secondline',$this->secondline,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('postcode',$this->postcode,true);
		$criteria->compare('phonenumber',$this->phonenumber,true);
		$criteria->compare('dateofbirth',$this->dateofbirth,true);
		$criteria->compare('upline',$this->upline);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}

0

#13 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 24 February 2012 - 03:32 PM

Have a look at your post array
if(isset($_POST['Users']))
                {
                    print_r($_POST);
                    die;
                        $model->attributes=$_POST['Users'];
                        if($model->save())
                                $this->redirect(array('view','id'=>$model->id));
                }


That might give you a clue, also you could do away with the datePicker widget and just use a text field until you are sure that everything else works.

So much for debugging strategies, if you want to shoot straight for the answer I think it is because you don't have a model defined in your widget.

Look here.

doodle
Check out myYii powered website
0

#14 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 04:31 PM

Weirdly the thing is posted. So why isn't it recognised?

Quote

Array ( [Users] => Array ( [username] => 42refno [password] => mercury [email] => example@example.com [firstname] => Sheridon [lastname] => Thompson [firstline] => 42 Cheque Street [secondline] => Hockley [city] => Birmingham [postcode] => B54 5Dh [phonenumber] => 0759491554 ) [dateofbirth] => 02/15/2012 [yt0] => Create )


View Postgot 2 doodle, on 24 February 2012 - 03:32 PM, said:

Have a look at your post array
if(isset($_POST['Users']))
                {
                    print_r($_POST);
                    die;
                        $model->attributes=$_POST['Users'];
                        if($model->save())
                                $this->redirect(array('view','id'=>$model->id));
                }


That might give you a clue, also you could do away with the datePicker widget and just use a text field until you are sure that everything else works.

So much for debugging strategies, if you want to shoot straight for the answer I think it is because you don't have a model defined in your widget.

Look here.

doodle

0

#15 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 24 February 2012 - 04:38 PM

View Postgot 2 doodle, on 24 February 2012 - 03:32 PM, said:

So much for debugging strategies, if you want to shoot straight for the answer I think it is because you don't have a model defined in your widget.

doodle


Nowhere on the CWidget API reference page, does it say how to do that, or that it can be done.
http://www.yiiframew...api/1.1/CWidget

I'm confused. :blink:
0

#16 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 24 February 2012 - 06:18 PM

Actual working code
<?php 
 $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model'=>$model,
     'attribute'=>'EventStartdate',
     // additional javascript options for the date picker plugin
     'options'=>array(
         'showAnim'=>'fold',
         'dateFormat'=>'yy-mm-dd',
         'defaultDate'
     ),
     'htmlOptions'=>array(
         'style'=>'height:20px;'
     ),
 ));
?>

This is defined as a date field
`EventStartdate` date NOT NULL DEFAULT '0000-00-00',


It's quite possible you are hung up on the slashes in the data in the post.

Quote

[dateofbirth] => 02/15/2012


Did you try just a text field to eliminate problems with the widget?

doodle
Check out myYii powered website
0

#17 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 28 February 2012 - 07:05 PM

I've done what you said with the widget, datetime and DATE mysql field, and it still doesn't work.
0

#18 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 28 February 2012 - 07:15 PM

Forget the widget, make sure that you can make it work with just a text field first.

Your example array ($_POST)
Array ( 
[Users] => Array ( 
  [username] => 42refno 
  [password] => mercury 
  [email] => example@example.com 
  [firstname] => Sheridon 
  [lastname] => Thompson 
  [firstline] => 42 Cheque Street 
  [secondline] => Hockley 
  [city] => Birmingham 	
  [postcode] => B54 5Dh 	
  [phonenumber] => 0759491554 
  ) 	
  [dateofbirth] => 02/15/2012 
  [yt0] => Create )


Notice that [dateofbirth] is not part of the [users] array.

That could be your clue.

You have $_POST['Users'] which is an array
You have $_POST['dateofbirth'] which has the value '02/15/2012'
And $_POST['yt0'] which is the Yii generated id for the submit button.


doodle
Check out myYii powered website
0

#19 User is offline   desbest 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 22-February 12

Posted 28 February 2012 - 07:33 PM

This topic is solved!
The problem was with the datepicker not being recognised despite using a correct name field, and for the controller function being bad.

I made a new Yii project from scratch, and then got it to work really quick.

Thanks a lot for your help doodle! :lol:

I will make new topics for other issues.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users