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

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.

Related

Doctrine addFilterConstraint on joined table

I'm using Doctrine Filters in my Symfony 3.x application.
Now, I need to filter a joined table, but the addFilterConstraint method allways receive the inheritance root:
In the case of joined or single table inheritance, you always get passed the ClassMetadata of the inheritance root. This is necessary to avoid edge cases that would break the SQL when applying the filters.
This is (part of) my class:
<?php
namespace AppBundle\Filter;
use Doctrine\ORM\Mapping\ClassMetaData;
use Doctrine\ORM\Query\Filter\SQLFilter;
class DataAbstractionFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
For example, if I had the classes:
class SomeClass {/**/}
class SomeChildClass1 extends SomeClass {/**/}
class SomeChildClass2 extends SomeClass {/**/}
addFilterConstraints will allways receive SomeClass.
Any suggestions? Thanks in advance.

How can I have an optional abstract method to avoid empty functions?

I currently have an abstract class which i am extending to other controllers. I have a abstract function within the abstract class which takes the value and places it in the __construct.
abstract class Controller extends BaseController {
abstract public function something();
public function __construct(Request $request) {
if (!is_null($this->something())){
$this->global_constructor_usse = $this->something();
}
}
}
My problem is that, on controllers that don't require this abstract function, I am having to place in the empty function.
class ControllerExample extends Controller {
public function something(){
return 'somethinghere';
}
}
Is there anyway to making the abstract function optional, or have a default value?
class EmptyControllerExample extends Controller {
public function something(){}
}
It is not possible to have a abstract method optional, as it is implied in PHP that all abstract methods must have an implementation.
There are legit use cases for optional abstract methods, yes: event handlers, metadata describers, etc. Unfortunately, you'll need to use regular, non-abstract methods with an empty body, and indicate in PHPDoc that they will do nothing unless extended.
Be wary, though: this can very quickly turn into code smell by diffusing a class responsability with their children. If you're dealing with generic events, you can look into Laravel's own event system, or the Observer pattern instead.
Abstract functions in a parent class, should only be used if its required by your application to implement the following method in all controllers who inherits from it, clearly it is not the case.
In this case i would make a trait. Here you create a trait which can be implemented by the classes who needs it. Notice the use keyword usage, use somethingTrait;
trait SomethingTrait
{
public function something()
{
echo "something called";
}
}
class Controller
{
use SomethingTrait;
public function run()
{
$this->something();
}
}
phpfiddle link
Another aproach could be doing a class inheritance structure, if the controllers you want to implement the methods has something in common. Where you would implement your special method in CrmController, where you still would be able to create shared methods in the abstract controller.
AbstractController
|
CrmController
|
CompanyController
For your question, 'Is there anyway to making the abstract function optional or have a default value?' No, and you are down the wrong path if you are trying to make abstract function optional. Hope my suggestions can help.

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

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().

Get the calling object from superclass

I've built a "model" superclass for a MVC framework. In most methods i do need only the class name so i've used get_called_class() but for save and edit methods i need to pass the object with values.
In my design, when you create a model object and you save or edit you have to do:
$object->save($object); or $object->update($object, $id).
I really don't like this, because looks as a bad design. I would like just to say:
$object->save(); and $object->update($id);
Since you are effectively saving the current object.
Models classes extends a Model parent that defines their behaviour and create the DB connection for them.
The methods of superclass that i would like to make does not take as an argument $object but rather i would like to say "get the calling object".
public function save($object) {
return self::$db->save($object);
}
public function update($object,$id) {
return self::$db->update($object, $id);
}
I know that this can be easily doable the in the object model with
public function save () {
parent::save($this);
}
But i would like not to have to reimplement this behaviour for every single model!
Thank you in advance.
The use of any existing ORM library isn't discussed here, since i want to provide a querybuilder and simple ORM that is PDO based as default. Because i do not want to have any 3rd party dependency as default
Define your base model as abstract class and inherit default behavior to child model classes.
abstract class Model
{
public function save() {
return self::$db->save($this);
}
public function update($id) {
return self::$db->update($this, $id);
}
}
class UserModel extends Model;
$myModel = new UserModel();
$myModel->save();

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