passing variable to view, kohana 3.3 not working, Undefined variable: - variables

i have a stupid question... why if i pass e variable to view the browser return me
Undefined variable: ? I just clone my first method (for the ads, the same procedure). But with ads work, and with category not work, this is so stupid, why?
i show my little application
my controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Ads extends Controller_Template {
public $template = 'template';
// function indes Ads
public function action_index()
{
$ads = ORM::factory('ads')->find_all(); // load all object inside ads table
$view = new View('ads/index'); // load the view/ads/index.php
$view->set('ads', $ads); // set 'ads' object to view
$this->template->set('content', $view);
}
// view single ads
public function action_single()
{
$id = $this->request->param('id');
$record = ORM::factory('ads')
->where('id_ads', '=', $id)
->find();
$view = new View('ads/single');
$view->set('ads', $record);
$this->template->set('content', $view);
}
public function action_category()
{
$category = ORM::factory('category')->find_all();
$view = new View('ads/index');
$view->set('category', $category);
$this->template->set('content', $view);
}
} // End Ads
my interest view (ads/index.php)
<?php foreach ($ads as $obj) : ?>
<h3><?php echo HTML::anchor('/ads/single/'.$obj->id_ads, $obj->title_ads); ?></h3>
<p><?php echo $obj->description_ads; ?></p>
<p>Autore: <?php echo $obj->author_ads; ?> || creato il <?php echo $obj->date_ads; ?> || categoria: <?php echo HTML::anchor('#', $obj->category->category_name); ?></p>
<?php endforeach; ?>
<?php foreach ($category as $obj) : ?>
<?php echo $obj->id; ?>
<?php endforeach; ?>
the error in the browser
ErrorException [ Notice ]: Undefined variable: category
why only for category ?? and not for ads?

Only one of action functions gets run,
This means in action_index() the $ads variable is being set but not $category.
And in action_category() the $category variable is being set but not $ads.
If you only expect to use of those variables, you should make another ads/category.php view and use that in the action_category() function, and remove the references to $ads in that view.
Also it seem that you are expecting the action_category() function to be running, based on the error you are seeing, it is actually running action_index(). Check your routes to find out why.

Related

Yii/Giix - Standard 2 Column Layout

The code below shows the two column layout in Yii. The $content variable holds a search form and a gridview form.
I'm trying to get the gridview to appear to the right of the Advanced Search Section in this two-column grid format. Kind of brain farting here, where in the standard Giix structure is the variable $content given it's contents? I didn't see it in the basemodel or controller.
Thanks in advance.
<?php /* #var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div class="span-24">
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
</div>
<div class="span-5 last">
<div id="sidebar">
<?php
$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'operations'),
));
$this->endWidget();
?>
</div><!-- sidebar -->
</div>
<?php $this->endContent(); ?>
$content is given its content when your controller call $this->render() at the end of its action.
public function actionIndex() {
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
[some code...]
$this->render('index');
}
The process involved is a bit obfuscated but you can easily trace it down by setting a breakpoint and looking at the stack in your debugger.
You can also read the code :
render() is a method of the CController class :
public function render($view, $data = null, $return = false) {
if ($this->beforeRender($view)) {
$output = $this->renderPartial($view, $data, true); // (1)
if (($layoutFile = $this->getLayoutFile($this->layout)) !== false)
$output = $this->renderFile($layoutFile, array('content' => $output), true); // (2)
[snip...]
}
}
(1) If no error occurs before rendering, the view is populated and its HTML code assigned to $output : $output = $this->renderPartial($view, $data, true);
(2) Then, unless you stated in your action that the view must not be decorated by the layout by colling $this->setLayout(false), the Decorator pattern is applied and the internal view set in the layout :
$output = $this->renderFile($layoutFile, array('content' => $output), true)
Here, you shall notice that the second argument is an array : array('content' => $output)
renderfile() is a method of CBaseController which, at some point, will call
public function renderInternal($_viewFile_, $_data_ = null, $_return_ = false) {
// we use special variable names here to avoid conflict when extracting data
if (is_array($_data_))
extract($_data_, EXTR_PREFIX_SAME, 'data'); // (1)
else
$data = $_data_;
if ($_return_) {
ob_start();
ob_implicit_flush(false);
require($_viewFile_); // (2)
return ob_get_clean();
}
else
require($_viewFile_);
}
And that's where your answer lies :
(1) $data is still our array('content' => $output). The extract function will build and initialize variables from this array, namely your $content variable.
(2) The layout file is now required. $content exists in its scope, as well, of course, as your controller wich lies behind $this
Use a grid layout in the specific view. It should something like
<div class='span-10'>
//search form
</div>
<div class='span-9'>
//grid
</div>

ZF2 render form with data from database

I want to edit a record from a database with a Zend Form (Zend Framework 2).
In ZF1 I did in my controller:
$values = $table->getValues();
$form = new MyForm();
$form->populate($values);
$this->view->form = $form;
and in the viewscript:
<?php echo $this->form ?>
In ZF2 I tried in my controller:
$values = $table->getValues();
$form = new MyForm();
$form->populateValues($values); // form->setData($values) does not work either
return array('form' => $form);
and in my viewscript:
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>
but that renders the form, without data however.
what is the correct way to do this?
You need to call prepare() on your form before you open it in your view script. For example:
<?php $form->prepare(); ?>
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>
See the reference guide here
Note the following point:
the prepare() method. You must call it prior to rendering anything in
the view (this function is only meant to be called in views, not in
controllers).
The issue was that $table->getValues() in my code returns an arrayObject whereas populateValues() expects an array.

validation_errors(), variable,

I want to save errors from validation_errors() in variable, and dispay it in view.
I know how I can pass variable to view pass another view:
in controller:
$data['date'] = 'some_data';
$this->load->view('register/register_open',$data);
in view_open
<?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view');
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?>
in view:
<?php echo $date; ?>
But I don`t know how can I save validation_errors() in array or variable, and pass then to view page.
Could you help me?
See this manual: http://ellislab.com/codeigniter/user_guide/general/views.html
Assigning variable:
<?php
class Blog extends CI_Controller {
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
Now using it in view as:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
In your case, try this in view:
var_dump($todo_list);
You will understand the data structure and how to process it.

Yii: model attribute's comments/hints

I need to have comments/hints for some fields in my Form. My idea is to describe it in model, just like attributeLabels. How can I do it?
And then it would be ideal, if the Gii Model (and Crud) generator would take it directly from mysql column's comment
So I see two questions here:
Describe hints in the model and display on the form.
Get hints from mysql comment
Displaying Hints from Model
Here's a slightly modified version of the default login.php file generated by Yiic
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<p class="hint">
Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
</p>
</div>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Let's move that password hint into the model by adding a attributeHints() method and a getHint() method to the LoginForm.php model.
/**
* Declares attribute hints.
*/
public function attributeHints()
{
return array(
'password'=>'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.',
);
}
/**
* Return a hint
*/
public function getHint( $attribute )
{
$hints = $this->attributeHints();
return $hints[$attribute];
}
As you can see, we;ve moved the hint text from the view into the model and added a way to access it.
Now, back in login.php, let's add the hint tag based on data from the model.
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
</div>
So now we've changed the hardcoded hint into a generated element populated with data from the model.
Now, moving on to the second question.
Getting hints from mySQL comments
Unforunately, I am not fammiliar enough with Gii to know how to automatically generate the hints from mySQL comments. However, getting the mySQL comment data into the model is fairly easy.
To do this we can use the following mySQL query
SHOW FULL COLUMNS FROM `tbl_user`
So let's add the comment to the password field
ALTER TABLE `tbl_user`
CHANGE `password` `password` VARCHAR( 256 )
CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL
COMMENT 'Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.';
And let's add the code to fetch it into our attributeHints() method.
/**
* Declares attribute hints.
*/
public function attributeHints()
{
$columns= Yii::app()->db->createCommand('SHOW FULL COLUMNS FROM `tbl_user`')->queryAll();
$comments=array();
foreach($columns as $column){
if( isset( $column['Comment'] ) )
{
$comments[ $column['Field'] ] = $column['Comment'];
}
}
//Add any hardcoded hints here
$hints = array(
'username'=>'Enter username above',
);
//Return merged array
return array_merge( $comments, $hints );
}
We now have two ways to set comments, through mySQL comments or through hard coded statements.
Let's update our login.php file quick to include hints of both types.
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
<?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('username')); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<?php echo CHtml::tag('p', array('class'=>'hint'), $model->getHint('password')); ?>
</div>
And that's it!
Now our login page will look like this, with a username hint from the model and a password hint from the mySQL comment.
As of Yii-1.1.13, a new attribute, comment, has been added to CMysqlColumnSchema which is defined in parent CDbColumnSchema:
comment of this column. Default value is empty string which means that no comment has been set for the column. Null value means that RDBMS does not support column comments at all (SQLite) or comment retrieval for the active RDBMS is not yet supported by the framework.
So we can use it to access the comment. Somewhat like this:
$modelObject->tableSchema->columns['column_name']->comment
// or if you are doing this within your model use
$this->tableSchema->columns['column_name']->comment
Note that the tableSchema is already set when any CActiveRecord model is constructed or even if the static model is used, so there are no new queries executed when we access the property.
A sample implementation within your model:
class MyModel extends CActiveRecord {
// hints array will store the hints
public $hints=array();
public function init() {
parent::init();
$this->hints=self::initHints($this);
}
public static function initHints($model) {
$comments=array();
foreach ($model->tableSchema->columns as $aColumn){
if(!empty($aColumn->comment))
$comments["$aColumn->name"]=$aColumn->comment;
}
return $comments;
}
public static function model($className=__CLASS__) {
$model=parent::model($className);
$model->hints=self::initHints($model);
return $model;
}
}
Now you can access hints as:
$model = new MyModel;
$single_column_hint=$model->hints['column_name'];
$model = MyModel::model();
$single_column_hint=$model->hints['column_name'];
For doing this through gii templates, you just need to copy the above code to your custom model code template, read the guide to see how this can be done.
I would recommend having the code in a custom base class, from which you can derive your active records.

YII - renderPartial gives error with CActiveForm

I am trying to include form on product page using renderPartial but it gives error
Fatal error: Call to a member function getErrors() on a non-object in /Applications/MAMP/htdocs/yii/framework/web/helpers/CHtml.php on line 1605
i am using below code...
in product page
// product detail goes here, use below form to make an inquiry about this product
<?php $this->renderPartial('inquiry'); ?>
and in inquiry page page
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'query-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
There is a variable called $model in your view, but in your controller's renderPartial() call, you are not passing $model into your view. So the view is looking for a variable called $model, but it does not exist.
You need to generate a new model in your controller, then pass it into the view, as follows:
$model = new Product(); //use whatever class you created for the model in place of 'Product' here
$this->renderPartial('inquiry', array('model'=>$model));
The 'model'=>$model tells Yii to pass the variable $model into the view, and the 'model' represents the name you use to access that variable from within the view. So if you write something like:
$this->renderPartial('inquiry', array('product'=>$model));
Then in the view, you would access the variable by typing $product instead of $model.