Additional model method doesn't work
inouet opened this issue · 7 comments
Code below doesn't work.
Hoge.php
<?php
require_once __DIR__.'/vendor/j4mie/idiorm/idiorm.php';
require_once __DIR__.'/vendor/j4mie/paris/paris.php';
ORM::configure('sqlite:sample.db');
class Foo extends Model
{
public static $_table = 'sample';
public function some_method()
{
echo "call: some_method()\n";
}
}
$foo = Model::factory("Foo");
$foo->some_method();
?>
$ php Hoge.php
PHP Fatal error: Uncaught exception 'IdiormMethodMissingException' with message 'Method some_method() does not exist in class ORMWrapper' in /vagrant/hoge/vendor/j4mie/idiorm/idiorm.php:2185
Stack trace:
#0 /vagrant/hoge/Hoge.php(19): ORM->__call('some_method', Array)
#1 /vagrant/hoge/Hoge.php(19): ORMWrapper->some_method()
#2 {main}
thrown in /vagrant/hoge/vendor/j4mie/idiorm/idiorm.php on line 2185
My code is something wrong ?
Version
j4mie/idiorm: v1.5.1
j4mie/paris : v1.5.4
Yes, there is. When you call ::factory()
it returns an instance of ORMWrapper
instead of Model
like you think it does.
I am not sure what you're trying to do inside some_method
so it's not clear which work around to use to fix this:
-
If creating a new query then you should use a static method
static function getUsers() { return Model::factory(__CLASS__)->find_many(); }
-
If you want to apply a set of filters then look into
filter()
In Docs you wrote:
Of course, because these objects are instances of your base model classes, you can also call methods that you have defined on them:
<?php
class User extends Model {
public function full_name() {
return $this->first_name . ' ' . $this->last_name;
}
}
$user = Model::factory('User')->find_one($id);
echo $user->full_name();
But this doesn't work at all. What's the right solution?
Please include a link to the related docs in your comment.
Are you getting an error? The code to power this interaction is there in the ORMWrapper
class of Paris.
( ! ) Fatal error: Uncaught exception 'IdiormMethodMissingException' with message 'Method full_name() does not exist in class ORM' in D:\xampp\htdocs\launch2.sm\Idiorm.php on line 2185
( ! ) IdiormMethodMissingException: Method full_name() does not exist in class ORM in D:\xampp\htdocs\launch2.sm\Idiorm.php on line 2185
OK, cool. Please open a new ticket as this is unrelated to this issue and this one is also closed. Please include all the information here.