Why do we use this "Post::model()" in Yii? - php

I am new to Yii so I don't know much, but I can tell that Post is the name of my Model class.
The following code contains this $models = Post::model()->findAll($criteria);

You class Post is a CActiveRecord class and in this class there is a
model method
http://www.yiiframework.com/doc/api/1.1/CActiveRecord
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#model-detail
model() Returns the static model of the specified AR class. CActiveRecord
Returns the static model of the specified AR class. The model returned is a static instance of the AR class. It is provided for invoking class-level methods (something similar to static class methods.)

Hii this method is written in your model. In your case it is in Post model and if you want to know more than it written in your yiilite.php file under your framework folder.
For more info read this
http://www.yiiframew...rd#model-detail
The static model returned by model() contains the db schema meta data regarding the class.
So we need to call model() to get the static model when we call the functions like find() and findAll().

Related

Does Laravel Framework instantiate every model class at boot?

With, where, hasMany etc. methods of Eloquent class are not static.
However we call these functions like that:
// Post is a child of Model class.
Post::where(...); // we don't use New keyword.
So, does Laravel Framework initiate all Model instances before we call their methods?
In the Eloquent's Model class, you have the below function which handles the static method calls dynamically.
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
As you can see, it creates an instance of the Model class on which a non static method is invoked statically, then invokes that method on the instance.

Laravel: How to set limit for my model class so that it can't be called directly?

The application I am working on has a wrapper(DAO) for each model class. The model itself is derived from Eloquent class. The issue is that junior developers keep calling methods like ModelClass:Where() or ModelClass::find() rather than using DAOClass:DaoMethod(). How can I restrict in my class that implementor of class can't do something like ModelClass::where()?
Thanks
The final keyword should do the trick. Just make all your desired DAOClass methods final. That way, once they are called from a class which extends your DAOClass, they will get a fatal error.

need clear php "->" symbol explanation in Yii CActiveRecord

i just learn yii framework, i need explanation about this part of code
$model=TblUser::model()->findByPk($id);
what i understand so far are:
$model is a variable
TblUser is class named TblUser.php
model() is static method
findByPk($id) is a method
is that right?, then i try to open model method inside TblUser class, but i cant find where findByPk() is located?, and what CLASS mean?
public static function model($className=__CLASS__)
{
return parent::model($className);
}
In Yii, every table model extends CActiveRecord class. CActiveRecord implements CRUD(Create, Read, Update, Delete) operations in the ORM(Object Relational Mapping) approach. So, findByPk, find, save, update, delete and so many other methods are parts of the CActiveRecord class.
When you use $model=TblUser::model()->findByPk($id), first, the static model() function inside the TblUser class will be called. model() function returns parent class of TblUser class(CActiveRecord in fact). And finally findByPk method of CActiveRecord will be executed.

Extended class does not see property of parent Codeigniter

I call a CodeIgniter controller method -imgupload- from jquery ajax. This controller extends my custom front controller.
class newad extends My_Controller{
public function __construct() {
parent::__construct();
}
public function imageupload() {
$this->load->library("uploadhandler");
}
The imgupload method calls the uploadhandler class which extends from newad.
class uploadhandler extends newad {
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected.
The inheritance chain looks like this: My_Controller->newad->uploadhandler.
Any idea why I cant access those properties?
In short the answer is you do not need to extend Controller class here. You can just pass the value to your library as a parameter.
$params = array('user_ud' => $this->userID, 'otehr' => 'other');
$this->load->library('uploadhandler', $params);
//Now from your library
class uploadhandler{
public function __construct($params)
{
// Do something with $params
}
//.. Your code...//
}
Now about your question:
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected. The inheritance chain looks like this: My_Controller->newad->uploadhandler. Any idea why I cant access those properties?
As inheritance chain are ok, you can access property of My_Controller from your library but not the value of the Current controller, because these two are different object.
So here is my answer how can we access the value? One way I have already mentioned. That will be enough if you need to share some property with the library. But what if you need to access all the Controller instance. There is a function to get the reference of controller instance get_instance(). You can use this function anywhere and get access of all public property controller. If you need to access any private property of controller the define a geter function to access that.
Explanation
First of all you need to learn basic about OOP. Learn about Class, Object, Inheritance..
When I said property of My_controller is different from the same property the you accessed from uploadhandler, it may confused you if you are not familiar with class and object. Here is two instance(object) of different class.
For short let say you have some classes like: Vehicle, Car, Machine and Person. All they have common attributes say name, weight ..
So, can we just inherit Any of these class from other??
Simple answer is no. We can't(!) define a Person class extending from Others. So how can we decide which incoherence would legal. If you can say Foo is a Bar you can write Foo class extending from Bar. Now from your case, It is obvious uploadhandler is not a controller. So Never Extend a anything from something that is not something.
NB: The answer is generic. If you need any specific clarification, just ask, I can update my answer

Function in model called automatically

If I give the same name to a function in the Model of Codeigniter, that function gets called automatically when I load the model.
//controller
$this->load->model('my_model');
//model
class My_model extends CI_model {
function my_model {
}
}
In this case I don't have to call my model function like this
$this->my_model->my_model();
because loading the model calls the function as well.
Can somebody explain this behaviour? I haven't found anything in docs regarding this.
This is a common concept in Object-Orientated programming. The function is acting as a Constructor. The constructor is called when an instance of the object is created.
In PHP using the __construct() method is the advised way to declare a constructor for the class. However, in PHP 4, a constructor was declared using the class name, so:
For backwards compatibility, if PHP 5 cannot find a __construct()
function for a given class, and the class did not inherit one from a
parent class, it will search for the old-style constructor function,
by the name of the class.
In CodeIgniter, a model is a class. As you don't have a __construct() method in your class, PHP is treating your my_model function as the constructor for the class (as it is the same as the class name).
You may want to the following method to your model. This will stop my_model being treating as a constructor.
function __construct()
{
// Call the Model constructor
parent::__construct();
}
I'd personally avoid calling a method the same name as the class in PHP, as it can lead to this confusion! PHP's docs have some useful information on constructors.

Categories