yii and b2evo: semi-integration notes

by prettyscripts on 2011-07-21 12:07

phpb2evolutionyii

i'm working on a yii-based project that uses b2evolution as cms.

the site already has a blog using b2evo. since i can't find something to use and instead of writing my own cms, i made use of b2evo's multi-blog engine to create a few new non-public blogs as content / documents for another subdomain that uses yii as front end.

i tried to create a new extension for the integration since there isn't one existing at this point of time. however, using codes from stub files to initialize b2evo in yii doesn't work because there are function call conflicts. it's probably due to each system has its own autoload, getter, setter etc functions. i'm unfamiliar with the core codes to understand how to make it work.

since the project i'm working on will use the same database, i just created a new module and new models to access tables used by b2evo and access data the yii's way.

note: if you are using different databases, there's limited multiple databases support in yii. i haven't had experience with this.

this isn't a full integration. it's just a note and some pseudo-codes how i make it work for my project.

please also note this is not a bridge. it's just a workaround solution.

assumptions

this note is based on the following assumptions:

  • the project and b2evo share the same database (different db probably works but i've never tried working on a yii-based project using more than 1 db)
  • each content (post) belongs to one category
  • the yii-project only displays the content. the content is still managed via b2evo.
  • no user integration between the yii and b2evo.

the basic b2evo module

i use gii to generate the inital module and the models.

basic models are required for the following tables:

  • evo_blogs
  • evo_categories
  • evo_item__items
  • evo_users – only if you need to display user info

evo_ is the table prefix defined in b2evo's config file. tables do not use the table prefix defined in Yii's config file.

add some commonly used methods to models. alternatively, create new models inheriting and extending these models.

relation rules

for models/B2evoBlog.php (evo_blogs table):

PHP:

public function relations()
{
    return array(
        'categories' => array(self::HAS_MANY
                'B2evoCategory''cat_blog_ID'),
        'owner' => array(self::BELONGS_TO
                'B2evoUser''blog_owner_user_ID'),
    );
}

for models/B2evoCategory.php (evo_categories table):

PHP:

public function relations()
{
    return array(
        'blog' => array(self::BELONGS_TO
                'B2evoBlog''cat_blog_ID'),
        'posts' => array(self::HAS_MANY
                'B2evoItem''main_cat_ID'),
        'publishedPosts' => array(self::HAS_MANY
                'B2evoItem''post_main_cat_ID',
                'condition' => 'item.post_status=\'published\'',
                'alias' => 'item'),
    );
}

note on line 10: optionally define constants for post status. check the enum values for post_status.

for models/B2evoItem.php (evo_item__items table):

PHP:

public function relations()
{
    return array(
        'category' => array(self::BELONGS_TO
                'B2evoCategory''post_main_cat_ID'),
        'author' => array(self::BELONGS_TO
                'B2evoUser''post_creator_user_ID'),
    );
}

for models/B2evoUser.php (evo_users table):

PHP:

public function relations()
{
    return array(
        'posts' => array(self::HAS_MANY
                'B2Item''post_creator_user_ID'),
    );
}

displaying the content

create a new module (optional) to handle display of these data.

create new models and extend those created above if you need to customize data for your project. create and edit new controller and view files, just like how you work on an yii project.

important: unless you know what you're doing, doing not use crud controller to manage any b2evo data. everything should be done via b2evo's backoffice.