Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call to a member function until() on null #126

Closed
leite opened this issue Apr 28, 2015 · 6 comments
Closed

call to a member function until() on null #126

leite opened this issue Apr 28, 2015 · 6 comments

Comments

@leite
Copy link

leite commented Apr 28, 2015

Hi,

Nice lib, I'm having a trouble setting up validation in an eloquent model, my enviroment as follows:

core/Model.php

namespace core;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent {

  use \Watson\Validating\ValidatingTrait;

  function errors () { return $this->getErrors(); }
}

the so called model in models/Users.php

namespace models;

class Users extends core\Model {
  public $timestamps    = true;
  protected $primaryKey = 'pk_user';
  protected $table      = 'users';
  protected $hidden     = array('password');
  protected $fillable   = array('name', 'nick', 'email');
  protected $rules      = array(
    'name'  => 'required|alpha_num|max:40',
    'nick'  => 'required|max:20',
    'email' => 'required|max:40|email'
  );
}

this is how I set up eloquent in core/Bootstrap.php

/* ... */
$capsule = new Illuminate\Database\Capsule\Manager();
$capsule->addConnection($config);
$capsule->setEventDispatcher(
  new Illuminate\Events\Dispatcher(new Illuminate\Container\Container)
);
$capsule->setAsGlobal();
$capsule->bootEloquent();
/* ... */

this is the composer.json

{
  "minimum-stability": "dev",
  "require": {
    "slim/slim":             "2.6.2",
    "zordius/lightncandy":   "dev-master",
    "mustangostang/spyc":    "0.5.1",
    "illuminate/database":   "5.0.27",
    "watson/validating":     "~1.0",
    "illuminate/events":     "5.0.*"
  },
  "autoload": {
    "psr-4": {
      "" : [
        "app/", "app/models/", "app/core/"
      ]
    }
  }
}

the problem is - everytime a call the seeder a get this error:

PHP Fatal error:  Call to a member function until() on null in /home/leite/studies/slim/vendor/illuminate/support/Facades/Facade.php on line 213

db/seeds/2015_04_27_21192206_users_seed.php

class UsersSeed {
  function run () {
    Users::truncate();

    $bcrypt = new core\Bcrypt(13);
    $user   = new Users();

    $user->name     = 'admin';
    $user->nick     = 'admin';
    $user->email    = '[email protected]';
    $user->password = $bcrypt->hash('m3u4u41m');

    $user->save();
  }
}

for the record:

~ ☺ php -v
PHP 5.6.1 (cli) (built: Oct  5 2014 23:44:08) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
@dwightwatson
Copy link
Owner

This would be because the library uses Laravel facades to dispatch the model events. Without using the Laravel container (I believe) the calls on the facades aren't forwarded to the right objects.

We could take a look at trying to replace the Facades in the package, but I'm not sure how we would be able to do that in a simple way. Unfortunately I don't have a good solution for using this package outside of Laravel. Happy to accept a PR that fixes this but it's not an issue for most of the users. Sorry!

@dwightwatson
Copy link
Owner

Actually, there was some discussion about dropping facades in #132, so might be worthwhile keeping an eye on that thread.

Also sorry for taking so long to respond, I must have missed the notification for this issue.

@karneaud
Copy link

karneaud commented Mar 5, 2016

wonder what @leite did to overcome this hurdle....I too am using Slim PHP

@karneaud
Copy link

karneaud commented Mar 5, 2016

Just adding this here. I found this package https://github.com/itsgoingd/slim-services ....but not sure how to incorporate the validation into it to make it work...any idea how your package can be integrated here using this?

@leite
Copy link
Author

leite commented Mar 5, 2016

nice @karneaud, I did nothing hehehe ... my app have validation on the front side and some double checks for uniqueness in the backend.

As you can see I am extending core\Model ... I guess you could create something like this and take advantage of slim-services

namespace core;

class Model extends Illuminate\Database\Eloquent\Model {

  private $_errors;

  function errors () {
    return $this->_errors;
  }

  static function boot () {

    parent::boot();

    static::saving(function ($model) {
      $validator = \Slim\Slim::getInstance()
        ->validator->make($model->attributes, $model->rules);
      if ($validator->fails()) {
        $model->_errors = $validator->errors();
        return false;
      }
      return true;
    });
  }

}

I would love to see the someone take some action about this (I'm kind busy at time), as to the author, please leave that issue open.

@karneaud
Copy link

karneaud commented Mar 5, 2016

I've been trying to see how I can adapt it but must admit my knowledge with facade pattern is young. Was not able to get it to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants