Home > Cakephp > How cakephp model load dynamically ?

How cakephp model load dynamically ?

How cakephp model load dynamically ?

  • Cakephp Model load dynamically without create model or $uses variable.
  • But sometime need without $uses variable for load model in another controller by dynamically.
  • When we want to load model dynamically then we can use this code .

<?php

class CategoriesController extends AppController

{

var $name = ‘Categories’;

function add()

{

$this->loadmodel($dynamicModelName);

if( $this->$dynamicModelName->save($this->data)){

$this->Session->setFlash(‘Your Data Save Successfully’,true);

$this->redirect(array(‘controller’=>’categories’,’action’=>’index’));

}

}

}

?>

Advertisements
Categories: Cakephp
  1. October 17, 2009 at 9:32 am

    The better approach is to use App::import(‘Model’, ‘ModelName’). So the code will look like this:

    App::import('Model', 'Posts');
    $this->Post = new Post();
    $posts = $this->Post->find('all');
    ...
    

    At least I am using that approach 🙂

    • arifhossen
      November 22, 2009 at 4:23 am

      Thank you very much for your better suggestion.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: