get_class() expects parameter 1 to be object, array given in Yii - php

i'm just research about Yii for 2 weeks but i have a project . the mission is use Yii Cactiveform to create a form. But I get this error : get_class() expects parameter 1 to be object, array given. This is my Controller:
public function actionPersonal()
{
// Layout
$this->layout = '../layouts/news_style';
// Title
$this->pageTitle = Yii::app()->params['news_title'];
// Description
$this->pageDescription = Yii::app()->params['news_description'];
// Breadcrumbs
$this->breadcrumbs = array(
'My Profile' => array(),
);
$layout = $this->_layout();
return $this->render('my_profile', array(
'user' => Yii::app()->user->getUser(),
'postDoXe' => $layout['postDoXe'],
'newPosts' => $layout['newPosts'],
));
}
my_profile.php
<div class="input-group">
<?php echo $form->label(Yii::app()->user->getUser(),'username'); ?>
<?php echo $form->textField(Yii::app()->user->getUser(),'username'); ?>
</div>

Yii::app()->user->getUser()
returns an array, but you need a model for your form elements, you have given an array for validation
<?php echo $form->textField(Yii::app()->user->getUser(),'username'); ?>
you need to populate a model using id returned from "Yii::app()->user->getUser()" and then use that model in your form
<?php echo $form->textField($model ,'username'); ?>

Related

How to pass data controller to view in codeigniter

I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.
this is my model
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
this is my controller:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
I am also trying to find a solution. I am passing user Id and another by URL (get method).
You are overwriting $data['query'] when you assign the array next:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
Either do:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
Or:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
Access in view via $query and $content.
Unrelated:
You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){
Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.
Controller
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
Then access it on your View file
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
Try doing something like this:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
YOUR MODEL
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
YOUR CONTROLLER
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
YOUR "template_master" VIEW
<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?>

Yii dropdownlist using $form-> dropdownlist

i want to create a form from 2 different models,
1st is for countries, and the 2nd is for documents.
The problem is that i can't make a dropdown list, i get the errors all the time.
Here's the code, first my controller.php part
$model = new Country;
$model2 = new Product;
$this->performAjaxValidation(array($model, $model2));
if(isset($_POST['Country'],$_POST['Product']))
{
// populate input data to $model and $model2
$model->attributes=$_POST['Country'];
$model2->attributes=$_POST['Product'];
// validate BOTH $model and $model2
$valid=$model->validate();
$valid=$model2->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$model->save(false);
$model2->save(false);
$this->redirect('index');
}
}
...
$countriesIssued = Country::model()->findAll(array('select'=>'code, name', 'order'=>'name'));
...
$this->render('legalisation', array('model'=>$model, 'model2'=>$model2, 'documents'=>$documents, 'countriesIssued'=>$countriesIssued, 'countries'=>$countries, 'flag'=>$flag));
}
In my view script I use this code
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->errorSummary(array($model,$model2)); ?>
<?php echo $form->dropDownList($model, 'countriesIssued',
CHtml::listData($countriesIssued, 'code', 'name'));?>
<?php $this->endWidget(); ?>
but i get this error :
Property "Country.countriesIssued" is not defined.
Ok it's not defined, i try to change it to 'countriesIssued', then i got another error saying Invalid argument supplied for foreach() .
If anybody can help me please.
I know there is more solutions on net, i try it but not understand, Thanks.
By definition, the first param of listData is an array; Your is a object;
<?php
echo $form->dropDownList($model, 'classification_levels_id', CHtml::listData(ClassificationLevels::model()->findAll(), 'id', 'name'),$classification_levels_options);
?>
Make a list variable like this:
In your Model:
$countriesIssued = Country::model()->findAll(array('select'=>'code, name', 'order'=>'name'));
And in your view:
$list = CHtml::listData($countriesIssued, 'code', 'name'));
echo CHtml::dropDownList('Your variable', Your $model,
$list,
array('empty' => '(Select a category'));
dropDownList($model,'state',array('1' => 'Do 1', '2' => 'Do 2'),array('selected' => 'Choose')); ?>
Or Yii 2
field($model,'state')->dropDownList(
array('1' => 'Do 1','0' => 'Do 2'),array('options' => array('0' => array('selected' => true))))
->label('State')
?>

Yii Collect Data For Two Or More Models ( Get_Class() Expects Parameter 1 To Be Object, Array Given )

I get this error when I call CreateController : "get_class() expects parameter 1 to be object, array given "
Controll/actionCreate() is as follows:
public function actionCreate() {
$model = new Ogrenci;
$model2 =new Adresler;
$this->performAjaxValidation($model, 'ogrenci-form');
$this->performAjaxValidation($model2, 'ogrenci-form');
if (isset($_POST['Ogrenci'],$_POST['Adresler'])) {
$model->setAttributes($_POST['Ogrenci']);
$model2->setAttributes($_POST['Adresler']);
if ($model->save(false) && $model2->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}
}
$this->render('create', array( 'model' => $model,'model2' => $model2));
}
create.php:
<?php $this->renderPartial('_form', array(
'model' => array('model'=>$model,'model2'=>$model2),
'buttons' => 'create'));
?>
And _form.php's fields is as follows:
<div class="row">
<?php echo $form->labelEx($model2,'aciklama'); ?>
<?php echo $form->textField($model2,'aciklama'); ?>
<?php echo $form->error($model2,'aciklama'); ?>
</div><!-- row -->
$this->renderPartial('_form', array(
'model' => array(
'model'=>$model,
'model2'=>$model2
),
'buttons' => 'create'
));
code above means that file _form.php will have access to two variables:
$model - array of two elements, and $buttons - string.
So, to get access to first model you have to write $model['model'], second - $model['model2'].
but in this code
<?php echo $form->labelEx($model2,'aciklama'); ?>
<?php echo $form->textField($model2,'aciklama'); ?>
<?php echo $form->error($model2,'aciklama'); ?>
You are trying to access undefined variable $model2. And this should raise respective error.
The thing that error is not got makes me thinking that somewhere before listed code you access variable $model in the similar way, something like this:
echo $form->labelEx($model,'test');
In the above code $model is array(because you passed array). That is why you receive error that object is expected.
So, you should either pass models or access them in a proper way.
I hope this helps.
I solved another problem.Perhaps someone may be in need..
(CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key)
if ($model->save(false) && $model2->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}
to
$a=$model2->save(false);
$model->adresler_id=$model2->adresler_id;
if ($a && $model->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}

link_to and passing variables

I have a page: my-account/my-templates/view/1 - which simply uses the id field of 1 to display the item
What I'm wanting to do is to create a copy action, that simply takes all the values in this item and creates a new item
I have a link_to() which holds the following:
<?php echo link_to('Copy', '#copy_template', array('id' => $template->getId())); ?>
I'm passing in the id of the template.
Can I access this id in the copy action?
EDIT:
Actions:
public function executeViewTemplate(sfWebRequest $request)
{
$this->template = Doctrine_Core::getTable('UserTemplate')->getUserTemplate($request->getParameter('id'));
}
public function executeTemplateCopy(sfWebRequest $request)
{
$this->id = $request->getParameter('id');
// get the passed template id
}
Templates:
viewTemplateSuccess.php
<?php echo link_to('Copy', '#copy_template', array('id' => $sf_request->getParameter('id'), 'class'=>'button green')); ?>
templateCopySuccess.php
<?php echo "ID". $id;?> --> doesn't return the passed id
In an action, you can retrieve a parameter using:
$id = $request->getParameter('id');
For example, if you have this action:
public function executeCopyTemplate(sfWebRequest $request)
{
$id = $request->getParameter('id');
}
And from a template:
<?php $id = $sf_request->getParameter('id') ?>
In your case:
<?php echo link_to('Copy', '#copy_template?id='.$sf_request->getParameter('id')); ?>
The array at the end of the link_to function alters HTML attributes like class and id. It doesnt pass url parameters normally. THis will work:
<?php echo link_to('Copy', '#copy_template?id='. $template->getId(), array()); ?>

Codeigniter passing data from controller to view

As per here I've got the following controller:
class User extends CI_Controller {
public function Login()
{
//$data->RedirectUrl = $this->input->get_post('ReturnTo');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('User_Login', $data);
}
//More...
}
and in my User_Login.php view file I do this:
<?php print_r($data);?>
which results in:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1
Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this), I can see a lot of stuff but none of my data except in caches
Edit: To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data variable is created in the scope of the view. I simply happened to use the same name in the controller
Ah, the $data array's keys are converted into variables: try var_dump($title); for example.
EDIT: this is done using extract.
you should do it like :
echo $title ;
echo $heading;
echo $message;
Or you can use it like array.
In Controller:
...
$this->load->view('User_Login', array('data' => $data));
...
In View:
<?php print_r($data);?>
will show you the Array ( [title] => My Title [heading] => My Heading [message] => My Message )
you can pass a variable in the url to
function regresion($value) {
$data['value'] = $value;
$this -> load -> view('cms/template', $data);
}
In the view
<?php print_r($value);?>
You can't print the variable $data as it is an associative array....you may print every element of the associative array.....consider the following example.
Don't do as follows:
echo $data;
Do as follows:
echo $title;
echo $heading;
echo $message;
You can use this way also
$data['data]=array('title'=>'value');
$this->load->view('view.php',$data);
while sending data from controller to view we pass it in array and keys of that arrays are made into variables by code codeigniter and become accessible in view file.
In your code below all keys will become variables in User_Login.php
class User extends CI_Controller {
public function Login()
{
$data = array(
'title' => 'My Title', //In your view it will be $title
'heading' => 'My Heading', //$heading
'message' => 'My Message' //$message
);
$this->load->view('User_Login', $data);
}
}
and in your User_Login.php view you can access them like this:
echo $title;
echo $heading;
echo $message;

Categories