0

The problem is that I import a model.

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;

And I re-import and instacing this model later. Normally it would work as well. However, this is a multi-language site. And the second instacing it returns with an empty string.

I tried this

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...
App::import('Model', 'Carrier');
$this->getCarrier = new Carrier;
... blabla...

and tried this:

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...
unset($this->Carrier);
App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...

Same result: the second instacing it returns with an empty string from database.

My translate model:

<?php
class Carrier extends AppModel
        {
        var $name = 'Carrier';
        public $actsAs = array('Translate' => array(
                                                'name',
                                                'description'
                                                     )
                                );
        }
?>

UPDATE:

Normally result:

Array ( [Carrier] => Array ( [id] => 1 [name] => TestCarrier [description] => Example  [status] => 1 ) )

Wrong result with re-imported model:

Array ( [Carrier] => Array ( [id] => 1 [name] =>  [description] => [status] => 1 ) )
EronarDiaras
  • 87
  • 1
  • 4
  • 13
  • Dont use `App::import('Model', 'Carrier'); $this->Carrier = new Carrier;`! You should always include your models using `ClassRegistry::init('Carrier')`. Also, no App::uses() needed. – mark Jun 25 '13 at 14:54
  • I tried: ClassRegistry::init('Carrier'); $this->Carrier = new Carrier; But the result same... :-( – EronarDiaras Jun 25 '13 at 19:36
  • It is good after all. I'm sorry. I used it wrong first. `ClassRegistry::init('Carrier')->findById(key($carriers));` Thank You!!! – EronarDiaras Jun 25 '13 at 20:13

1 Answers1

1

You should not use App::import(). That is only for vendor classes. Internally, its App::uses().

But for models, this does not apply, either. Simply use ClassRegistry::init():

$Carrier = ClassRegistry::init('Carrier');
$results = Carrier->find(...);

For models cake has its own loading mechanism.

mark
  • 21,691
  • 3
  • 49
  • 71