Fork me on GitHub

Models

This set of classes, which is designed to work with a database.

For testing and verification implemented three technologies work with database:

Consider the main features of these technologies through the establishment of a new message (post).

In directory app/Models/ created a set of classes for working with database. Provider app/Providers/ModelsServiceProvider.php provides access to these classes using the method load.

public function load($modelName, $modelMethod = null, $data = array()) {
    if (isset($this->app["models.{$modelName}"])) {
        $Model = $this->app["models.{$modelName}"];
    } else {
        $modelClass = "\\Models\\{$modelName}Model";
        $Model = new $modelClass();
        $Model->setApp($this->app);
        $this->app["models.{$modelName}"] = $Model;
    }
    return $Model->$modelMethod($data);
}

In our case we will use the class of model PostModel app/Models/PostModel.php to work with post table in the database app/Resources/db/app.db.

Example recourse to the model can be seen here:

// Create new post
$models->load('Post', 'newPost', array('username' => $username, 'new_post' => $ormPost));

Doctrine(DBAL)

Example of adding record to the table post using the method newPost of class PostModel app/Models/PostModel.php you can see here.

/**
 * newPost
 * Add post for current user
 * 
 * @param array $data
 * @return void
 */
public function newPost($data) {
    $db = $this->app['db'];
    //--------------------

    $username = $data['username'];
    $ormPost = $data['new_post'];
    // Transform the date to format 'Y-m-d'
    $date = new \DateTime($ormPost->getCreated());
    $ormPost->setCreated($date->format('Y-m-d'));
    $arrPost = $ormPost->getValues();

    // Get user's id
    $sql = "SELECT * FROM user WHERE username = ?";
    $user = $db->fetchAssoc($sql, array($username));
    if ($user === FALSE) {
        $this->app->abort(404, "Not find user for this username \"{$username}\"");
    }
    $arrPost['user_id'] = $user['id'];
    $db->insert('post', $arrPost);
}

Here, the variable $ormPost is a data object of class Post app/Models/ORM/Post.php, which is based on technology Doctrine(DBAL).

Examples of data objects for operation with database app/Resources/db/app.db can be viewed here app/Models/DBAL/.

Examples of work with the database using the console commands can be seen here app/Cosole/Commands/DBAL/.

Doctrine(ORM)

Example for obtaining all messages (posts) for the user can be seen here.

This is done using the method getPostsForUser of class PostModel app/Models/PostModel.php.

/**
 * getPostsForUser
 * Get posts for user
 * 
 * @param string $userName
 * @return array
 */
public function getPostsForUser($userName) {
    $em = $this->app['em'];
    $repo = $em->getRepository('Models\ORM\User');
    //--------------------
    $user = $repo->findOneByUsername($userName);

    if ($user === NULL) {
        $this->app->abort(404, "Not find user for this username \"{$userName}\"");
    }
    $posts = $user->getPosts();

    // Data for user's posts
    $data = array('username' => $user->getUsername(), 'posts' => $posts->toArray());

    return $data;
}

Examples of data objects for operation with database app/Resources/db/app.db can be viewed here app/Models/ORM/.

Examples of work with the database using the console commands can be seen here app/Cosole/Commands/ORM/.

PHP ActiveRecord

Example for obtaining all messages (posts) can be seen here.

This is done using the method postsAction of class TestController app/Controllers/TestController.php.

/**
 * Action - test/posts
 * receive posts written by the user
 * 
 * @param string $userName 
 * @param int $page 
 * @return string
 */
public function postsAction($userName, $page) {
    $app = $this->app;
    $db = $this->app['ar'];
    //--------------------
    try {
        ...
        // Get posts
        if ($userName) {
            ...
        } else {
            $posts = array();
            // Get user's posts
            $users = $db->find('user', 'all', array('select' => 'id, username'));
            foreach ($users as $user) {
                $posts[$user->username] = $user->post;
            }
            // Data for user's posts
            $data = array('posts' => $posts);
        }
        return $this->showView($data);
    } catch (\Exception $exc) {
        return $this->showError($exc);
    }
}

If you login to application as Admin, you can access to this example, via menu Testing/PHP ActiveRecord/User posts.

Examples of data objects for operation with database app/Resources/db/app.db can be viewed here app/Models/AR/.