In Laravel I can write
class MyModel extends Model {
public function getTextAttribute($value) {
return strtoupper($value);
}
}
And then
$model = MyModel::find(1);
$model->text = 'test';
echo $model->text; // returns 'TEXT'
Can I do similar in Yii2?
Add a public var in your MyModel and you can redifine the text getter this way
class MyModel extends Model {
public $text;
public function getText() {
return strtoupper($this->text);
}
}
.
$model= MyModel::findOne(1);
$model->text = 'test';
echo $model->text;
Related
I want to change automatically the data returns from the model in CodeIgniter 4.
That's what comes to my mind, but it's not works:
class UserModel extends Model {
protected $afterFind = ['getImage'];
protected function getImage(array $data) {
if ($data['user_image'] == 0) {
$data['user_image'] = "New Value";
}
return $data;
}
}
Anyone have an idea how to do that?
Thanks.
Solution:
class UserModel extends Model {
protected $afterFind = ['getImage'];
protected function getImage(array $data) {
if ($data['data']['user_image'] == 0) {
$data['data']['user_image'] = "New Value";
}
return $data;
}
}
I have some models belong to Activity Model.
in my Activity.php I had
<?php
class Activity extends \Eloquent {
protected $fillable = [];
public function activity_car_nums()
{
return $this->hasMany('ActivityCarNum');
}
public function newables()
{
return $this->hasMany('Newable');
}
public function serial_codes()
{
return $this->hasMany('SerialCode');
}
public function applys()
{
return $this->hasMany('Apply');
}
}
and in SerialCode.php, I had
<?php
class SerialCode extends \Eloquent {
protected $fillable = ['code'];
public function activity()
{
return $this->belongsTo('Activity');
}
}
and in my controller, when I wrote
$serial_codes = [];
while(count($serial_codes) < $serial_code_total)
{
$code = substr(md5(uniqid(rand(), true)),0,5);
$serial_code = new SerialCode(['code' => $code]);
if(!in_array($serial_code, $serial_codes))
{
$serial_codes[] = $serial_code;
}
}
$activity->serial_codes()->saveMany($serial_codes);
it works.
But when it turns to
//this can get activity object
$activity = Activity::find($id);
//this can get the serial codes of the object above.
$serial_codes = SerialCode::whereActivityId($id)->get();
//this don't work, it returns null
$serial_codes = $activity->serial_codes;
for I really don't know why...
Can anybody help me please, and sorry for my poor English. Thank You.
(If you need any code else please tell me.)
my model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class Product extends Model
{
public $table="s_products";
protected $primaryKey = 'product_id';
public function reviews()
{
return $this->hasMany('App\ProductReview');
}
public function attributes()
{
return $this->hasMany('App\AttributeMapping');
}
public function images()
{
return $this->hasMany('App\Image');
}
}
I found the reason that why my model query cannot work, because the "_" in function name.
just change
public function serial_codes()
to
public function serialcodes()
then everything will go fine.
Thank to everybody.
My model is Patient->Sample->Ready_Sample ,relationship all is oneToMany ,
My question is I query Ready_Sample need to know patient.name
Patient_Model
class Patient_Model extends Base_Model {
protected $table = 'patients';
public function samples(){
return $this->hasMany('App\Models\Sample_Model','patient_id','code');
}
}
Sample_Model
class Sample_Model extends Base_Model{
protected $table = 'samples';
public function patient(){
return $this->belongsTo('App\Models\Patient_Model','patient_id','code');
}
public function ready_samples(){
return $this->hasMany('App\Models\Ready_Sample_Model','sample_id','code');
}
}
Ready_Sample_Model
class Ready_Sample_Model extends Model{
protected $table = 'ready_samples';
public function sample(){
return $this->belongsTo('App\Models\Sample_Model','sample_id','code');
}
}
In Sample_Controller
class Sample_Controller extends Controller{
public function query(Request $request){
$result = Sample_Model::with(['patient']);
->orderBy("updated_at","desc")
->Paginate(15)
->toJson();
return $result;
}
In Sample I know to get patient.name ,but Ready_Sample how to get Patien.name?
You can get the patient.name using the below code:
$readySample = Ready_Sample_Model::first(); // fetch the first record
echo $readySample->sample->patient->name;
Hope it helps!
I have function showItemList inside User class
class User extends Eloquent {
//...
protected $item = ['axe', 'sword', 'knife'];
public function showItemList() {
return $this->$item;
}
}
In my controller it's possible to use this.
$id = 1;
$user = User::find($id);
$user -> showItemList();
But how can I just call this function directly(irrelevant to $id query)?
I looks for something like below (sure now it's not working):
$list = User::showItemList();
You need to use the static protected variable and return it from static method.
class User extends Eloquent {
//...
static protected $item = ['axe', 'sword', 'knife'];
public static function showItemList() {
return self::$item;
}
}
I'm new to PHP framework (using Yii framework). How to get a value in MySQL then display it at a view? I'm confused in defining a model, controller, and how to use it.
Model:
public $name;
public $info;
$product = product::find()->orderBy('name')->all();
public function tablename()
{
return 'productdata';
}
Controller:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function LoadModel($id){
$model=productdata::model()->find($id);
return $model;
}
Have you tried any tutorials?
[models/MyModel.php]
class MyModel extends CActiveRecord {
public function rules() {
return ['id, name, value', 'safe'];
}
}
[controllers/MyController.php]
class MyController extends CController {
public function actionView($id) {
$model = MyModel::model()->findByPk($id);
$attributes = Yii::app()->request->getParam('MyModel');
if ($attributes) {
if ($model->save()) {
$this->redirect('myController/admin');
} else {
throw new CHttpException(500, 'Model not saved. Use echo CActiveForm::validat($model);');
}
}
$this->render('view', ['model' => $model]);
}
}
[views/MyController/view.php]
<?php echo $model->name; ?>