Property not defined in CActiveDateProvider - yii

Controller:
<?php
$dataProvider=new CActiveDataProvider('ClubMember', array(
'criteria'=>array(
'select'=>'Count(`fb_user_id`) as total, YEAR(`registerdate`) as year,registerdate',
'group' =>'WEEK(`registerdate`), YEAR(`registerdate`)',
'order' =>'registerdate DESC',
'condition'=>"t.club_id = '$club'",
),
'pagination'=>array(
'pageSize'=>10,
),
));
?>
View page:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_dispaly',
)); ?>
View Partial page:
<?php echo $data->year; ?>
When I tried to get $data->year, I got the following error:
Property "ClubMember.year" is not defined.
How can I access the property 'year' from above?

In your class you have to add them as public properties
class ClubMember extends CActiveRecord
{
public $year;
public $total;
....
...
}

Related

Can't get id from a foreign key

can i know where is my fault
Error
insert into `images` (`product_id`, `image`, `updated_at`, `created_at`) values (?, 1656838354.png, 2022-07-03 08:52:34, 2022-07-03 08:52:34)
My Models
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
use HasFactory;
protected $fillable = [
'image' , 'product_id'
];
public function product()
{
return $this->belongsTo('App\Product' , 'product_id');
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'title',
'description',
'image',
'detail',
'price',
'quantity'
];
public function image()
{
return $this->hasMany('App\Image');
}
}
My Controller
public function store(Request $request)
{
$request->validate([
'image'=>'required|mimes:jpg,png,jpeg|max:4096',
]);
$newImgName = time() . '.' . $request->image->extension();
$request->image->move(public_path('multiple_images'), $newImgName);
$image = Image::create([
'product_id' => $request->id,
'image' => $newImgName,
]);
return redirect()->route('products.index')
->with('msg','Record has been inserted successfully');

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into posts (title, body, updated_at, created_at) values (?, ?, 2019-10-14 17:41:00, 2019-10-14 17:41:00))
controller (CRUD) controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\post;
class postcontroller extends Controller
{
public function store(Request $request)
{
$add = new post;
$add->title= $request->input('title');
$add->body= $request->input('body');;
$add->save();
session()->flash('msg','new post added successfully!');
return redirect()->route('posts.show',$add->id);
}
Model (post model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
protected $fillable = ['title', 'body'];
}
view ->(it just a from using laravelcollective )
It looks like you are sending null value for the title and to catch out the validation errors earlier on in your application, you should consider introducing some validation in your controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\post;
class postcontroller extends Controller
{
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$add = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);
session()->flash('msg','new post added successfully!');
return redirect()->route('posts.show', $add->id);
}
Make sure you have set the title and body fields in your form correctly
<form><!-- / Dont forget the action, crf and other required attr of form -->
<input type="text" name="title">
<textarea name="body"></textarea>
</form>
Learn more about form validation at https://laravel.com/docs/master/validation#validation-quickstart

How to show content for the users using CGridView?

I have a page with the list of categories on my site and I want to show them using Cgridview as right now they displayed with CListView. I'm using Yii 1.1. I see that CGridView used only if you want to manage content but is it possible to use it if you want to show it, on the index pages?
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'category-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'status',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
You can used this code for your cgridview
Categories Model
File Path : protected/model/categories.php
Categories.php
<?php
/**
* This is the model class for table "categories".
*
* The followings are the available columns in table 'categories':
* #property integer $id
* #property string $title
* #property string $status
*/
class Categories extends CActiveRecord
{
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'area';
}
/**
* #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('title, status', 'required'),
array('status', 'numerical', 'integerOnly'=>true),
array('title', 'length', 'max'=>255),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, title, status', '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',
'title' => 'Title',
'status' => 'Status'
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* #return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('title',$this->title,true);
$criteria->compare('status',$this->status);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* #param string $className active record class name.
* #return Area the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
Categories Controller
File Path : protected/model/CategoriesController.php
CategoriesController.php
<?php
class CategoriesController extends Controller
{
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* #return array access control rules
* #access Public
*/
public function accessRules()
{
return array(
array('allow',
'actions' => array('index'),
'users' => array('*'),
),
array('deny',
'users' => array('*'),
),
);
}
public function actionIndex()
{
$model = new Categories();
$model->unsetAttributes();
if(isset($_REQUEST['Categories']))
$model->attributes = $_REQUEST['Categories'];
$this->render('index',array('model'=>$model));
}
}
Categories View File
File Path : protected/view/index.php
index.php
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'category-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'status',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
you can add your design in index.php file.
refere this link for design

Yii set user for every action in controller

i try to set user for every action in controller.
I have acces rules,
and i was set 2 diferent menu for admin and quest.
But I have one action which is not in any menu.
Default user for actions in yii is quest, so if I don't mentioned action in menu for admin, user for that action will be quest.
I don't want to mentioned that action in menu (layout) but i want to user for that action be admin.
In theory I want to set user for every individual action.
public function actionTest()
{
!Yii::app()->user->isGuest;
...
}
Something similar this but this is not work.
MORE CODE:::
I have controller TestController
I have layout main.php
I have login form, and action logout.
So next:
My controller:
class TestController extends Controller
{
/**
* Declares class-based actions.
*/
public function accessRules()
{
return array(
array('allow',
'actions'=>array('login'),
'users'=>array('*'),
),
array('allow',
'actions'=>array('test1','test2','logout','test3'),
'users'=>array('#'),
),
);
}
public function actionLogin()
{
$this->render('login');
}
public function actionTest1()
{
$this->render('test1');
}
public function actionTest2()
{
$this->render('test2');
}
public function actionTest3()
{
$this->render('test3');
}
public function actionLogout()
{
...
}
}
In layout main.php i have criteria for menu:
if(Yii::app()->user->isGuest)
{
$this->widget('bootstrap.widgets.TbNavbar',array(
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbMenu',
'items'=>array(
array('label'=>'Login', 'url'=>array('/test/login')),
),
),
),
));
}
else
{
$this->widget('bootstrap.widgets.TbNavbar',array(
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbMenu',
'items'=>array(
array('label'=>'Test1', 'url'=>array('/test/test1')),
array('label'=>'Test2', 'url'=>array('/test/test2')),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/test/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
),
),
));
}
Like you can see I did not set test3 in 'else' but in test 2 I set redirect to test3 and after login test1 and test2 user will not be quest but test3 will because that is default.
In this situation it is not syntax error.

Yii inherit attributeLabels

With Yii php framework, I use inheritance.
In my AbstractModel, I have this method:
public function attributeLabels()
{
return array(
'0'=>Yii::t('default','No'),
'1'=>Yii::t('default','Yes'),
);
}
In my object who extends AbstractModel, I have this method:
public function attributeLabels()
{
return array(
'username' => Yii::t('user', 'email'),
);
}
In a view file, I use:
<?php echo CHtml::activeLabel($model, $model->property);?>
But I never show 'No' or 'Yes' from asbtractModel. If I put all in my model it works. But I want to use inheritance.
How can I concat parent attributeLabels with current model attributeLabels?
Simply merge the return value of the parent method in MyObject (model class):
public function attributeLabels() {
return array_merge(
parent::attributeLabels(),
array(
'username' => Yii::t('user', 'email'),
)
);
}
You may also use CMap::mergeArray().