nova-framework / framework

Demo application using Nova - this is an extensive playground, use "app" repository for real applications.
http://novaframework.com/
MIT License
418 stars 211 forks source link

Native database #478

Closed jimgwhit closed 8 years ago

jimgwhit commented 8 years ago

@daveismyname and @LuckyCyborg have you seen the pull request on the fluentpdo github site?
If we are not using doctrine dbal, could we not use these packages either? If everything is written by dave or @LuckyCyborg or @tomvlk then updating the framework will be easier. As of now I have to look for updates in nova and fluentpdo to stay up to date. I know you want to move forward and finalize, but give some thought to it please.

jimgwhit commented 8 years ago

@LuckyCyborg I see in demo a Post model, no post controller. 😑

LuckyCyborg commented 8 years ago

@jimgwhit Jim, is is a Post Model, for sure.

But, to understand that you have to create a instance of the Model, for it to work. There are not possible static method calls, i.e. Post::find(1),

Look at my previous post. I given you examples.

jimgwhit commented 8 years ago

@LuckyCyborg Thanks and @nekomajin thanks.

LuckyCyborg commented 8 years ago

@jimgwhit Remember that what we have us into \Nova\ORM is a Model with Relations.

LuckyCyborg commented 8 years ago

@jimgwhit Okay, then now you have also the ability to call the findX methods into static way. So, now will work as expected something like:

$post = Post::find(7);
ghost commented 8 years ago

I'm getting an error with the latest commit for 3.0.

Exception information:

       Date: Feb 09, 2016 22:31PM

       Message: Argument 1 passed to Nova\Database\Statement::__construct() must be an instance of Nova\Database\PDOStatement, instance of PDOStatement given, called in /home/kingorgg/projects/framework/system/Database/Connection.php on line 232

       Code: 0

       File: /home/kingorgg/projects/framework/system/Database/Statement.php

       Line: 38

If it helps I'm using ORM, please see my test controller below.

namespace App\Controllers;

use Nova\Core\View;
use App\Core\BaseController;
use Nova\ORM\Model as BaseModel;

use App\Models\Users;

use Nova\Net\Request;
use Nova\Net\Session;

use \PDO;

/**
* Login Class
*/
class Login extends BaseController
{

    private $basePath;
    private $model;

    /**
     * Call the parent construct
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new Users();
    }

    protected function beforeFlight()
    {
        $this->basePath = str_replace(BASEPATH, '', $this->viewsPath());

        // Leave to parent's method the Flight decisions.
        return parent::beforeFlight();
    }

    protected function afterFlight($result)
    {
        // Leave to parent's method the Flight decisions.
        return parent::afterFlight($result);
    }

    public function index()
    {    

        $user = new Users();
        $user->username = 'virgil';
        $user->email = 'virgil@novaframework.dev';
        $result = $user->save();

        $viewName = 'index';
        $filePath = $this->basePath.$viewName.'.php';
        $data['title'] = 'Login';
        View::renderPage($viewName, $data, 'themed');
    }

    public function validate()
    {

    }
}
LuckyCyborg commented 8 years ago

@Kingorgg You are kind to show me your Users model?

The problem which you show is interesting; look to

http://smvc3.giulianaeassociati.com/demos/models/orm_model

You have there a very detailed example about using the native ORM models. Also, as a reference ORM model implementation, see App\Modules\Demo\Models\User

BTW, the ORM models name should be at singular. Then, is wanted User, not Users

ghost commented 8 years ago

My Users Model looks like this (Renamed so they are no longer plural):

<?php

namespace App\Models;

use Nova\ORM\Model as BaseModel;

class User extends BaseModel
{
    protected $table = 'users';

    protected $relations = array('posts', 'characters', 'roles', 'threads');

    public function __construct()
    {
        parent::__construct();
    }

    public function posts()
    {
        return $this->hasMany('App\Models\Post', 'user_id');
    }

    public function characters()
    {
        return $this->hasMany('App\Models\Character', 'user_id');
    }

    public function roles()
    {
        return $this->belongsToMany('App\Models\Role', 'users_roles', 'user_id', 'role_id');
    }

    public function threads()
    {
        return $this->hasMany('App\Models\Thread', 'user_id');
    }
}

and here is the stack trace if it helps:

Stack trace:

#0 /home/kingorgg/projects/framework/system/Database/Connection.php(232): Nova\Database\Statement->__construct(Object(PDOStatement), Object(Nova\Database\Driver\MySQL))
#1 /home/kingorgg/projects/framework/system/Database/Query/Builder.php(235): Nova\Database\Connection->prepare('INSERT INTO `sr...')
#2 /home/kingorgg/projects/framework/system/Database/Query/Builder.php(454): Nova\Database\Query\Builder->statement('INSERT INTO `sr...', Array)
#3 /home/kingorgg/projects/framework/system/Database/Query/Builder.php(488): Nova\Database\Query\Builder->doInsert(Array, 'insert')
#4 [internal function]: Nova\Database\Query\Builder->insert(Array)
#5 /home/kingorgg/projects/framework/system/ORM/Builder.php(185): call_user_func_array(Array, Array)
#6 /home/kingorgg/projects/framework/system/ORM/Model.php(692): Nova\ORM\Builder->__call('insert', Array)
#7 /home/kingorgg/projects/framework/app/Controllers/Login.php(54): Nova\ORM\Model->save()
#8 [internal function]: App\Controllers\Login->index()
#9 /home/kingorgg/projects/framework/system/Core/Controller.php(146): call_user_func_array(Array, Array)
#10 /home/kingorgg/projects/framework/system/Net/Router.php(227): Nova\Core\Controller->execute()
#11 /home/kingorgg/projects/framework/system/Net/Router.php(255): Nova\Net\Router->invokeController('App\\Controllers...', 'index', Array)
#12 /home/kingorgg/projects/framework/system/Net/Router.php(295): Nova\Net\Router->invokeObject('App\\Controllers...', Array)
#13 /home/kingorgg/projects/framework/system/Config/bootstrap.php(80): Nova\Net\Router->dispatch()
#14 /home/kingorgg/projects/framework/public/index.php(78): require('/home/kingorgg/...')
#15 {main}
pandory-network commented 8 years ago

@Kingorgg pleace test this pull request https://github.com/simple-mvc-framework/framework/pull/606

ghost commented 8 years ago

@pandory-network Thank you! Your pull request fixed my problem. ^_^

tomvlk commented 8 years ago

Great news :smiley: