Yii Framework Forum: How can get model instance dynamic? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How can get model instance dynamic? Rate Topic: -----

#1 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 16 January 2010 - 11:17 PM

i have a component.in this component,i want get model instance from model,like this:

component
<?php
class TestComponent extends CComponent
{
	public $model;

	public function __construct($model)
	{
	   $this->model = $model;
	}
 
    public function test()
    {
        $user = $this->model::model()->findAll();
    }
}


model
    public function test()
    {
        $tree = new TestComponent('User');
        $tree->test();
    }


there has a error,i don't know how to make model work in the TestComponent,please give me a idea,thanks!
0

#2 User is offline   dreamynsx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 22-February 09

Posted 17 January 2010 - 12:59 AM

Try:

$model = CActiveRecord('User');

$users = $model->findAll()
0

#3 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 17 January 2010 - 04:33 AM

thank you !
but seens not work
0

#4 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 17 January 2010 - 06:44 AM

class A
{
    public static function b()
    {
        echo 'wee!';
    }
}

$a = 'A';
$a::b();


Works only in PHP 5.3 + ...

Try

$model = CActiveRecord::model($this->model);
$model->findAll();

1

#5 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 17 January 2010 - 08:09 AM

great,it's work,thank you!!!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users