Search code examples
phpactiverecordormphpdocphpactiverecord

Phpdoc documentation with php activerecord


I'm using php-activerecord for a short while now and i absolutely love it. Php-activerecord is an open source ORM library based on the ActiveRecord pattern. However, i also like phpdoc and use that to document my code. Therefore it's easy for my coworkers to understand the platform's i build. But with php-activerecord in use my Model classes now look like this:

/**
 * The Company class.
 *
 * @since 1.0.0
 *
 */
class Company extends \ActiveRecord\Model
{
    /** explicit table name since our table is not "company"  */
    static $table_name = 'Company';

    /** explicit pk since our pk is not "id" */
    static $primary_key = 'companyId';
}

They work, but they used to look like this:

/**
 * The Company class.
 *
 * @since 1.0.0
 *
 */
class Company extends \ActiveRecord\Model
{
    /** @var integer The company id. */
    private $companyId;

    /** @var string The company name. */
    private $name;
}

Long story short

With php-activerecord in use there's no way to document my model attributes and update phpdoc. I want to be able to do this, in what direction should i look?


Solution

  • You can document all your "magic" properties as an @property! Say you have a table "Company" with fields "id, name, location", you would end up with:

    **
     * @property int $id
     * @property string $name
     * @property string $location
     */
    class Company extends \ActiveRecord\Model 
    {
    
    }
    

    You can see in the documentation that there are some other tricks, like "property-read". I use those for any connections you might have, as you can read a has-one for instance, but cannot write models to those connections.

    So if a Company has employees that you have defined as a $employees, you might add

    * @property-read Employee[] $employees
    

    and so on.