I'm stuck using the old yii.
Its active record model can be used as follows:
class Booking extends CActiveRecord {
/**
* @param string $className
* @return Booking
*/
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function publish(){
// code to publish the booking
}
// ... the rest of the generated code for the table
}
$model = Booking::model();
$model->getUnallocated();
$booking = $model->findByPk(815);
$booking->publish();
the problem is that the IDE (PhpStorm) does not allow me to ctrl+click the publish function in $booking->publish()
because it does not know that the value returned by findByPk is a Booking instance.
I can fix this like follows
class Booking extends CActiveRecord {
/**
* @return Booking|null
*/
public function findByPk($pk,$condition='',$params=array())
{
return parent::findByPk($pk,$condition,$params);
}
// ... the rest of the class
}
The problem is that this solution is not a clean one, now I have to define every fetching function find
, findAll
, findByPk
... in every AR model class.
Another way to do it is like this
/** @var Booking $booking */
$booking = $model->findByPk(815);
But this has to be defined whenever it's used, which is also bothersome because it's used in many places.
Is there a clean way to this without adding as many method definitions?
Time has passed, I found a solution
The trick is that you can return @return static
in the comments, which is exactly what you need for PHPStorm to detect the correct class, so lets create a BaseModel
that extends CActiveRecord
and has the correct doc comments
So change class MyClass extends CActiveRecord
with class MyClass extends BaseModel
while using this, now all the class hinting will work :)
<?php
/**
* Extension of the default Yii Active Record class
*
* Edit this class to add custom behaviour to all the models used within the application
*
*/
class BaseModel extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* Uses the class that called the model() by default
* @param string $className active record class name.
* @return static The static model class instance
*/
public static function model($className = null)
{
return parent::model($className !== null ? $className : get_called_class());
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param mixed $pk
* @param string $condition
* @param array $params
* @return static
*/
public function findByPk($pk,$condition='',$params=array())
{
return parent::findByPk($pk,$condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param string $condition
* @param array $params
* @return static
*/
public function find($condition='',$params=array())
{
return parent::find($condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param string $condition
* @param array $params
* @return static[]
*/
public function findAll($condition='',$params=array())
{
return parent::findAll($condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param $pk
* @param string $condition
* @param array $params
* @return static[]
*/
public function findAllByPk($pk,$condition='',$params=array())
{
return parent::findAllByPk($pk,$condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param $attributes
* @param string $condition
* @param array $params
* @return static
*/
public function findByAttributes($attributes,$condition='',$params=array())
{
return parent::findByAttributes($attributes,$condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param $attributes
* @param string $condition
* @param array $params
* @return static[]
*/
public function findAllByAttributes($attributes,$condition='',$params=array())
{
return parent::findAllByAttributes($attributes,$condition,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param $sql
* @param array $params
* @return static
*/
public function findBySql($sql,$params=array())
{
return parent::findBySql($sql,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* @param $sql
* @param array $params
* @return static[]
*/
public function findAllBySql($sql,$params=array())
{
return parent::findAllBySql($sql,$params);
}
/**
* This function exists only to help the IDE autodetect the returned class
* This will add a couple of milliseconds with every `with` usage
* @return static
*/
public function with()
{
return call_user_func_array('parent::with', func_get_args());
}
/**
* This function exists only to help the IDE autodetect the returned class
* @return static
*/
public function together()
{
return parent::together();
}
}