Skip to main content

displayField different than 'name'

http://book.cakephp.org/view/438/displayField

The displayField attribute specifies which database field should be used as a label for the record. The label is used in scaffolding and in find('list') calls. The model will use name or title, by default.

For example, to use the username field:

Plain Text View

class User extends AppModel {
var $displayField = 'username';
}
  1. class User extends AppModel {
  2. var $displayField = 'username';
  3. }

To top

cake bake Call to undefined function mysql_connect()

On this situation:

htdocs/cake/cake/console$ ./cake bake -app /var/www/localhost/htdocs/nikoz/ProdNG2/

Welcome to CakePHP v1.2.4.8284 Console
---------------------------------------------------------------
App : ProdNG2
Path: /var/www/localhost/htdocs/nikoz/ProdNG2
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> m
---------------------------------------------------------------
Bake Model
Path: /var/www/localhost/htdocs/nikoz/ProdNG2/models/
---------------------------------------------------------------

Fatal error: Call to undefined function mysql_connect() in /var/www/localhost/htdocs/cake_1.2.5/cake/libs/model/datasources/dbo/dbo_mysql.php on line 374

 

to solve do:

sudo ./cake bake -app /var/www/localhost/htdocs/nikoz/ProdNG2/

 

To top

appunti

Application layout:

css files, images, javascript

app/webroot/css/
app/webroot/img/
app/webroot/js

Dafault layout :
app/views/layouts/default.ctp

default route:

app/config/routes.php:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

 

convenzioni file in app:

index.php

config:
acl.ini.php
bootstrap.php
core.php
#configurazione dell'accesso al DB (hostname, driver, username, password, etc)
database.php
inflections.php
routes.php
sql

#i nomi dei controllers sono il plurale dei nomi dei modelli seguiti da _controller.php
controllers:
components
labels_controller.php
###############################################
class LabelsController extends AppController {

        var $name = 'Labels';
        var $scaffold;
        
        public function sindex() {
###############################################
panels_controller.php
###############################################
class PanelsController extends AppController {

        var $name = 'Panels';
        var $scaffold;
        
        public function sindex() {
###############################################

locale:
eng

#i nomi dei modelli sono al singolare (relativi alle tabelle del DB plurali)
models:
behaviors
datasources
label.php
###############################################
class Label extends AppModel {

        var $name = 'Label';
###############################################
panel.php
###############################################
class Panel extends AppModel {

        var $name = 'Panel';
###############################################
plugins:
empty

tests:
cases
fixtures
groups

tmp:
cache
logs
sessions
tests

vendors:
shells

views:
elements
errors
helpers
layouts
pages
scaffolds

webroot:
css
css.php
favicon.ico
files
img
index.php
js
test.php

To top

cake bake (example)

cd /srv/www/htdocs/
tar xfvj cake_1.2.0.7296-rc2.tar.bz2
mv cake_1.2.0.7296-rc2 cake
cp -Rv cake/app/ patch_panels
vim
patch_panels/webroot/index.php

insert in the begin of the file:

define('CAKE_CORE_INCLUDE_PATH', '/path to/cake');

chmod -R ugo+rw patch_panels/tmp/
cd cake/cake/console/

on mac osx with MAMP:

sudo ln -sf /Applications/MAMP/tmp/mysql /var/

with leopard and MAMP 1.8.4:
sudo ln -sf  /Applications/MAMP/tmp/mysql/mysql.sock /tmp

 

cake bake -app /path to/patch_panel/
####################### DB CONFIGURATION ######################


Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
App : app
Path: /srv/www/htdocs/patch_panels/app
---------------------------------------------------------------
Your database configuration was not found. Take a moment to create one.
---------------------------------------------------------------
Database Configuration:
---------------------------------------------------------------
Name:  
[default] >
Driver: (db2/firebird/mssql/mysql/mysqli/odbc/oracle/postgres/sqlite/sybase)
[mysql] >
Persistent Connection? (y/n)
[n] >
Database Host:  
[localhost] >
Port?  
[n] >
User:  
[root] > patch_panel
Password:  
> patch_panel
Database Name:  
[cake] > patch_panel
Table Prefix?  
[n] >
Table encoding?  
[n] >

---------------------------------------------------------------
The following database configuration will be created:
---------------------------------------------------------------
Name:         default
Driver:       mysql
Persistent:   false
Host:         localhost
User:         patch_panel
Pass:         ***********
Database:     patch_panel
---------------------------------------------------------------
Look okay? (y/n)
[y] > y
Do you wish to add another database configuration?  
[n] > n

Creating file /srv/www/htdocs/patch_panels/app/config/database.php
Wrote /srv/www/htdocs/patch_panels/app/config/database.php

################# MODELS CONFIGURATION ####################
cake bake

Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
App : app
Path: /srv/www/htdocs/patch_panels/app
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> m
---------------------------------------------------------------
Bake Model
Path: /srv/www/htdocs/patch_panels/app/models/
---------------------------------------------------------------
Possible Models based on your current database:
1. Label
2. Panel
Enter a number from the list above, type in the name of another model, or 'q' to exit  
[q] > 1
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > n
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > y
One moment while the associations are detected.
---------------------------------------------------------------
Please confirm the following associations:
---------------------------------------------------------------
Label hasOne Panel? (y/n)
[y] > n
Label hasMany Panel? (y/n)
[y] > y
Would you like to define some additional model associations? (y/n)
[n] > n

---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Label
Associations:
            Label hasMany    Panel
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Baking model class for Label...

Creating file /srv/www/htdocs/patch_panels/app/models/label.php
Wrote /srv/www/htdocs/patch_panels/app/models/label.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > n
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> m
---------------------------------------------------------------
Bake Model
Path: /srv/www/htdocs/patch_panels/app/models/
---------------------------------------------------------------
Possible Models based on your current database:
1. Label
2. Panel
Enter a number from the list above, type in the name of another model, or 'q' to exit  
[q] > 2
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > n
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > y
One moment while the associations are detected.
---------------------------------------------------------------
Please confirm the following associations:
---------------------------------------------------------------
Panel belongsTo Label? (y/n)
[y] > y
Would you like to define some additional model associations? (y/n)
[n] > n

---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Panel
Associations:
            Panel belongsTo Label
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Baking model class for Panel...

Creating file /srv/www/htdocs/patch_panels/app/models/panel.php
Wrote /srv/www/htdocs/patch_panels/app/models/panel.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > n
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> q


################# CONTROLLERS CONFIGURATION ####################
cake bake

Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
App : app
Path: /srv/www/htdocs/patch_panels/app
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> c
---------------------------------------------------------------
Bake Controller
Path: /srv/www/htdocs/patch_panels/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Labels
2. Panels
Enter a number from the list above, type in the name of another controller, or 'q' to exit  
[q] > 1
---------------------------------------------------------------
Baking LabelsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] > y
Would you like to use scaffolding? (y/n)
[n] > y

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:  Labels
                   var $scaffold;
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Creating file /srv/www/htdocs/patch_panels/app/controllers/labels_controller.php
Wrote /srv/www/htdocs/patch_panels/app/controllers/labels_controller.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > n
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> c
---------------------------------------------------------------
Bake Controller
Path: /srv/www/htdocs/patch_panels/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Labels
2. Panels
Enter a number from the list above, type in the name of another controller, or 'q' to exit  
[q] > 2
---------------------------------------------------------------
Baking PanelsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] > y
Would you like to use scaffolding? (y/n)
[n] > y

---------------------------------------------------------------

The following controller will be created:
---------------------------------------------------------------
Controller Name:  Panels
           var $scaffold;
---------------------------------------------------------------
Look okay? (y/n)
[y] > y

Creating file /srv/www/htdocs/patch_panels/app/controllers/panels_controller.php
Wrote /srv/www/htdocs/patch_panels/app/controllers/panels_controller.php
Cake test suite not installed.  Do you want to bake unit test files anyway? (y/n)
[y] > n
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
> q

To top

ACL

create your ACL database tables

../cake/cake/console/cake schema run create DbAcl -app ../test

Welcome to CakePHP v1.2.0.7692 RC3 Console
---------------------------------------------------------------
App : testPath: /srv/www/htdocs/cake_1.2.0.7692-rc3/../test
---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
The following table(s) will be dropped.
acos
aros
aros_acos

Are you sure you want to drop the table(s)?
(y/n) [n] > y
Dropping table(s).
acos updated.
aros updated.
aros_acos updated.
The following table(s) will be created.
acos
aros
aros_acos

Are you sure you want to create the table(s)?
(y/n) [y] > y
Creating table(s).
acos updated.
aros updated.
aros_acos updated.
End create.

To create the aro groups

 ../cake/cake/console/cake  acl create aro 0 Users -app ../test
 New Aro 'Users' created.

../cake/cake/console/cake  acl view aro -app ../test

Aro tree:
---------------------------------------------------------------
  [1]Users
---------------------------------------------------------------

in users controller (users_controler.php) add:

<?php
class UsersController extends AppController

 var $components = array('Acl');

function register()
    {
        if(!empty($this->data)){
            if($this->User->save($this->data)){
                $this->Session->setFlash('registration OK');
                $this->Session->write('user', $this->data['User']['username']);
                $parent = $this->Acl->Aro->findByAlias('Users');
                $this->Acl->Aro->create(array(
                        'alias' => $this->data['User']['username'],
                        'model' => 'User',

                        'foreign_key' => $this->User->id,
                        'parent_id' => $parent['Aro']['id'])
                );
                $this->Acl->Aro->save();
            } else {
                $this->data['User']['password'] = '';
                $this->Session->setFlash('There was a problem saving
                                   this information');
            }
            $this->redirect(array('action'=>'index'),null,true);
        }
    }

 

 to check aro:

 ../cake/cake/console/cake  acl view aro -app ../test

Aro tree:
---------------------------------------------------------------
  [1]Users

    [3]pillolo

  [2]Dealers

---------------------------------------------------------------

to add an already created user:

../cake/cake/console/cake  acl create aro Users nikoz -app ../test

Aro tree:
---------------------------------------------------------------
  [1]Users

    [3]pillolo

    [4]nikoz

  [2]Dealers

---------------------------------------------------------------

 

To top

21 things on Cakephp

http://www.avatarfinancial.com/pages/cake/

 

What's Cake?

Cake is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. CakePHP is basically the PHP equivalent of Ruby on Rails.

What should I read first?

If you are new to CakePHP, I highly recommend reading through the CakePHP manual and the 15 minute blog tutorial in addition to the items listed here. There are a ton of tutorials in the wiki, too.

Why the list?

CakePHP is very new, so documentation beyond the basics is a bit sparse in areas. I built this website entirely using CakePHP and took notes along the way to share with others. While some of these items can be found in obvious places like the manual, others required a bit of elbow grease to figure out. My name is Matt Inman and your feedback is welcome, feel free to email me with questions or comments.

 

  • Easily creating static pages

    I needed to create several pages that didn't use any models and contained static data inside the default layout. My first thought was to create a controller for these pages and define an action for each static page I needed. However, this solution seemed tedious and would make it difficult to quickly add new pages. Enter the pages controller - simply create a view inside the views/pages/ folder and it'll automatically be rendered in /pages. For example, if I created /views/pages/matt.thtml it would be accessible via www.example.com/pages/matt

  • Static pages - Adjusting the page title

    If you're using the pages controller and you need to change the page title, add the following to your view:
    <? $this->pageTitle = 'Title of your page.'; ?>

  • Static pages - Adjusting other data sent to the layout

    If you need to send data to the layout (such as a variable indicating what section to highlight on the nav bar), add this to your view:

    <? $this->_viewVars['somedata'] = array('some','data'); ?>

    That array should then be accessible as $somedata inside your layout.

  • Creating a simple admin center

    If you need to create an administrative back-end for your CakePHP site and would like all the actions with administrative capabilities to exist under a specific folder, open up config/core.php and uncomment:
    define('CAKE_ADMIN', 'admin');

    This will then make all actions that are prefixed with "admin_" to be accessible via:
    /admin/yourcontroller/youraction. For instance, if I created an action in my posts controller called "admin_add," I would access this via: www.example.com/admin/posts/add
    From there I could simply password the admin folder to prohibit unwanted users from adding posts.

  • Viewing the SQL queries that are running behind the scenes

    You can easily see the SQL queries that CakePHP is running by adjusting the DEBUG constant in config/core.php. 0 is production, 1 is development, 2 is full debug with SQL, and 3 is full debug with SQL and dump of the current object. I typically have debug set at 2, which renders a table at the bottom of the page that contains SQL debug information.

    If rendering a table at the bottom of your site is constantly breaking your layout during development (especially if you're making AJAX calls and you're getting SQL inside your pages, not just the bottom), you can easily style this table to be hidden by adding this to your CSS:

    #cakeSqlLog { display: none; }

    This will allow you to view debug information in the HTML source code without your layout getting mangled, just don't forget to set debug back to 0 when your site goes live.

  • Multiple sources of documentation

    Don't just rely on the manual. The wiki and the API are invaluable sources of information. The tutorials in the wiki are especially useful, and the API may be daunting at first, but you'll quickly find the information in there is crucial to building a site with CakePHP.

  • Using bake.php

    Bake is a command line PHP script that will automagically generate a model, controller, and views based on the design of your database. I highly recommend using scaffolding to get a prototype going of a table that may change a lot in the beginning. If you're fairly certain the data is not subject to any drastic change, I recommend using bake instead. With bake all the files are generated and written to disk and you can make modifications from there. It saves a lot of time doing the repetitive tasks such as creating associations, views, and the basic CRUD controller operations.

    Using bake is really easy. Once you have a table(s) in your database created, change directories to the /cake/scripts/ folder and run:
    php bake.php

    If you choose to bake interactively it'll walk you through the steps required to create your model, controller, and views. Once everything has been baked I usually go through all the generated code and make custom modifications as needed.

  • Mind permissions when moving cake around

    When I changed from the development server to the live server I tarred up my entire cake directory and scp'd it to the new server. Immediately I started having an issue where any time the debug level was set to 0 (production mode), data would not be returned for certain database calls. This was a bit of a catch 22 since I needed to view debug information to troubleshoot the problem.
    Someone in #cakephp kindly pointed out that permissions on the /app/tmp folder need to be writeable by apache. I changed the permissions to 777 and the issue went away.

  • Complex model validation

    I needed to validate beyond just checking to make sure a field wasn't empty or it matched a regular expression. In particular, I needed a way to verify that the email address users registered with was unique. In the wiki I found this gem: this advanced validation tutorial, which covers some advanced methods of validation that were very useful.

  • Logging errors

    $this->log('Something broke');
    This will log your error to /tmp/logs/ (I initially made the mistake of thinking it would log it to the apache error log)

  • Creating a controller that uses other models

    Suppose you have a controller that needs data from a bunch of different models, simply add this to the top of your controller:

    class yourController extends AppController
    {
    var $uses = array('Post','User');
    }


    This controller would then have access to both the Post and the User model.

  • Creating a model for a table that doesn't actually exist in the database

    I needed a way to create a model and controller without actually having an associated table in the database. I particularly wanted to make use of the $validate array so I could easily validate my fields and keep the validation logic in the model. CakePHP will throw an error if you create a model for a table that doesn't exist. Adding this to the model fixed the problem:
    var $useTable = false;

    You can use this to change tables names as well.
    var $useTable = 'some_table';

  • Call exit() after redirecting

    This should be no surprise to anyone who has done any serious web development in the past, but make sure you call exit() after running $this->redirect() if there's code afterward that you don't want to run. I've always done this in the past, but I made the assumption that $this->redirect() would make an exit call for me (which it didn't).

  • Advanced model functions

    Unless you delve in to the API, there are some very useful model functions at your disposal you might not know exist. I highly recommend reading over the Model Class Reference at least once. Here's a few key functions I wasn't aware of that I found to be very useful:
    • generateList() - I use this function primarily to populate select boxes with data from associated tables
    • query() - Sometimes you just need to write your own SQL
    • findCount() - Returns number of rows matching given SQL condition
    • hasAny() - Returns true if a record that meets the given conditions exists.
    Again, I highly recommend reading over the entire model class reference, you'll be surprised at what you learn.
  • Inserting multiple rows in succession

    I had a situation where I needed to iterate through a list of items and insert new rows for each. I quickly discovered that if you insert an item and then immediately insert another, the item that is inserted next doesn't insert at all. Instead the previously inserted row was being updated. For example:

    $items = array('Item 1','Item 2','Item 3');
    foreach ($items as $item) {
      $this->Post->save(array('Post' => array('title' => $item)));
    }


    This code will result in a single entry in the posts table: "item 3." CakePHP inserted "item 1", but then updates it to become "item 2," then "item 3" because $this->Post->id gets the value of the last inserted ID. Normally this functionality is very useful, but in this particular instance it was not. I found was to setting $this->Post->id = false after each insert solved the problem.

    Update: Someone emailed me and apparently the proper way of doing this is to call create() to initialize the model and then set/save your new data.

  • Inserting logic before or after controller functions

    Suppose you needed an array of colors to be available to every view rendered by your controller but you don't want to have to define this data in every action. Using the beforeRender() callback will allow you to do this:

    function beforeRender() {
      $this->set('colors',array('red','blue','green');
    }


    This would make $colors accessible in every view rendered by that controller. beforeRender() is called after the controller logic and just before a view is rendered.
    There's also beforeFilter() and afterFilter(), which are called before and after every controller action. For more information, read up on callbacks in the models section of the manual.

  • Adding a WYSIWYG editor to CakePHP

    I found this great tutorial on getting TinyMCE set up with CakePHP. Basically you just link the tiny_mce .js file to your page and then add a small bit of init code to every page that you want textareas to be converted into TinyMCE editors.

  • Writing your own SQL for HABTM relationships

    I had an issue with trying to create a HABTM (has-and-belongs-to-many) relationship where I needed to specify my own SQL statement. According to the docs (at the time of this writing) you should set finderSql in your model, but according to the cakePHP source you should set finderQuery instead. It's just a foul-up in the docs, but I figured it'd be worth noting to save others from having to figure it out for themselves. Trac ticket here: https://trac.cakephp.org/ticket/1217

  • Sending email

    I found two tutorials in the wiki: Sending email and Sending email with PHPMailer
    I highly recommend the latter of the two, sending emails with PHPMailer is more secure and there's less of a headache because you don't have to deal with constructing the mail headers yourself.

  • Customizing HTML generated by the Helper

    I needed to change the default <option> generated when I called $html->selectTag() to say something like "Please Select" rather than an empty space (default). I also wanted radio buttons to have labels so the user doesn't have to click exactly on the radio button itself but can instead click anywhere on the text associated with it.

    Create the file /app/config/tags.ini.php and add the following:
    ; Tag template for a input type='radio' tag.
    radio = "<input type="radio" name="data[%s][%s]" id="%s" %s /><label for="%3$s">%s</label>"

    ; Tag template for an empty select option tag.
    selectempty = "<option value="" %s>-- Please Select --</option>"


    You can get a full list of available tags in /cake/config/tags.ini.php. I wouldn't recommend modifying that file, however, because you could lose your changes when you upgrade CakePHP.

  • Creating a custom 404 error page

    If you need to change the page that users see when a document is not found, create:
    /app/views/errors/error404.thtml

About this Article

This document was last modified on September 14, 2006 and was written by Matt Inman
Avatar Financial Group is a hard money lender based in Seattle, WA.

 

To top

Multiple Display Field

 

http://bakery.cakephp.org/articles/view/multiple-display-field-3

 

Multiple Display Field

Behaviors Oct 22, 2008

By Daniel Albert (resshin)

The behavior allows us to use multiple display field (such as "first_name" and "last_name") as a display field for generating list from a model.

This is really one of common issues: using multiple fields as display field when using the find('list') function.

Strangely enough, there's only little hint out there about how to do it "elegantly" in CakePHP. tclineks already make a great article in http://bin.cakephp.org/saved/19252#modify, but the code just won't works. I think it's happened because CakePHP now handles find function differently than the previous versions.

So then I wrote the code below to help anyone who need to use multiple fields in their display field.
Note: The afterFind function is taken without any change from tclineks' bin.

Using it is as simple as this:

  1. In your model, define that the model is acting as MultipleDisplayFieldBehavior. For example, see Figure 1.
  2. To generate a list, simply use the find('list') function. For example, see Figure 2.


Note: you must define the "displayField" property. This field (which can be a virtual/non-existent field) will then holds the concatenation of display fields that you defined.

Download code
class User extends AppModel {
    var $name = "User";
    var $displayField = "full_name";
    var $actsAs = array('MultipleDisplayFields' => array(
        'fields' => array('first_name', 'last_name'),
        'pattern' => '%s %s'
    ));
}
Figure 1

Download code
$userList = $this->User->find("list");
Figure 2

The MultipleDisplayFieldsBehavior class is defined as below:

Model Class:

Download code<?php 
class MultipleDisplayFieldsBehavior extends ModelBehavior {
    var 
$config = array();
    
    function 
setup(&$model$config = array()) {
        
$default = array(
            
'fields' => array($model->name.'.first_name'$model->name.'.last_name'),
            
'pattern' => '%s %s'
        
); 
        
$this->config[$model->name] = $default;
        
        if(isset(
$config['fields'])) {
            
$this->config[$model->name]['fields'] = $config['fields'];
        }
        if(isset(
$config['pattern'])) {
            
$this->config[$model->name]['pattern'] = $config['pattern'];
        }
    }
    
    function 
afterFind(&$model$results) {
        
// if displayFields is set, attempt to populate
        
foreach ($results as $key => $val) {
            
$displayFieldValues = array();

            if (isset(
$val[$model->name])) {
                
// ensure all fields are present
                
$fields_present true;
                foreach (
$this->config[$model->name]['fields'] as $field) {
                    if (
array_key_exists($field,$val[$model->name])) {
                        
$fields_present $fields_present && true;
                        
$displayFieldValues[] = $val[$model->name][$field]; // capture field values
                    
} else {
                        
$fields_present false;
                        break;
                    }
                }

                
// if all fields are present then set displayField based on $displayFieldValues and displayFieldPattern
                
if ($fields_present) {
                    
$params array_merge(array($this->config[$model->name]['pattern']), $displayFieldValues);
                    
$results[$key][$model->name][$model->displayField] = call_user_func_array('sprintf'$params );
                }
            }
        }
        return 
$results;
    }


    function 
beforeFind(&$model, &$queryData) {
        if(isset(
$queryData["list"])) {
            
$queryData['fields'] = array();
            
            
//substr is used to get rid of "{n}" fields' prefix...
            
array_push($queryData['fields'], substr($queryData['list']['keyPath'], 4));
            foreach(
$this->config[$model->name]['fields'] as $field) {
                
array_push($queryData['fields'], $model->name.".".$field);
            }
        }
        
$model->varDump($queryData);
        return 
$queryData;
    }
}
?>

Hope it helps you... :)

To top