CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeErrorController
  • Component
  • ComponentCollection
  • Controller
  • Scaffold
  1: <?php
  2: /**
  3:  * Scaffold.
  4:  *
  5:  * Automatic forms and actions generation for rapid web application development.
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       Cake.Controller
 18:  * @since         Cake v 0.10.0.1076
 19:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 20:  */
 21: App::uses('Scaffold', 'View');
 22: 
 23: /**
 24:  * Scaffolding is a set of automatic actions for starting web development work faster.
 25:  *
 26:  * Scaffold inspects your database tables, and making educated guesses, sets up a
 27:  * number of pages for each of your Models. These pages have data forms that work,
 28:  * and afford the web developer an early look at the data, and the possibility to over-ride
 29:  * scaffolded actions with custom-made ones.
 30:  *
 31:  * @package       Cake.Controller
 32:  */
 33: class Scaffold {
 34: 
 35: /**
 36:  * Controller object
 37:  *
 38:  * @var Controller
 39:  */
 40:     public $controller = null;
 41: 
 42: /**
 43:  * Name of the controller to scaffold
 44:  *
 45:  * @var string
 46:  */
 47:     public $name = null;
 48: 
 49: /**
 50:  * Name of current model this view context is attached to
 51:  *
 52:  * @var string
 53:  */
 54:     public $model = null;
 55: 
 56: /**
 57:  * Path to View.
 58:  *
 59:  * @var string
 60:  */
 61:     public $viewPath;
 62: 
 63: /**
 64:  * Name of layout to use with this View.
 65:  *
 66:  * @var string
 67:  */
 68:     public $layout = 'default';
 69: 
 70: /**
 71:  * Request object
 72:  *
 73:  * @var CakeRequest
 74:  */
 75:     public $request;
 76: 
 77: /**
 78:  * Valid session.
 79:  *
 80:  * @var boolean
 81:  */
 82:     protected $_validSession = null;
 83: 
 84: /**
 85:  * List of variables to collect from the associated controller
 86:  *
 87:  * @var array
 88:  */
 89:     protected $_passedVars = array(
 90:         'layout', 'name', 'viewPath', 'request'
 91:     );
 92: 
 93: /**
 94:  * Title HTML element for current scaffolded view
 95:  *
 96:  * @var string
 97:  */
 98:     public $scaffoldTitle = null;
 99: 
100: /**
101:  * Construct and set up given controller with given parameters.
102:  *
103:  * @param Controller $controller Controller to scaffold
104:  * @param CakeRequest $request Request parameters.
105:  * @throws MissingModelException
106:  */
107:     public function __construct(Controller $controller, CakeRequest $request) {
108:         $this->controller = $controller;
109: 
110:         $count = count($this->_passedVars);
111:         for ($j = 0; $j < $count; $j++) {
112:             $var = $this->_passedVars[$j];
113:             $this->{$var} = $controller->{$var};
114:         }
115: 
116:         $this->redirect = array('action' => 'index');
117: 
118:         $this->modelClass = $controller->modelClass;
119:         $this->modelKey = $controller->modelKey;
120: 
121:         if (!is_object($this->controller->{$this->modelClass})) {
122:             throw new MissingModelException($this->modelClass);
123:         }
124: 
125:         $this->ScaffoldModel = $this->controller->{$this->modelClass};
126:         $this->scaffoldTitle = Inflector::humanize(Inflector::underscore($this->viewPath));
127:         $this->scaffoldActions = $controller->scaffold;
128:         $title = __d('cake', 'Scaffold :: ') . Inflector::humanize($request->action) . ' :: ' . $this->scaffoldTitle;
129:         $modelClass = $this->controller->modelClass;
130:         $primaryKey = $this->ScaffoldModel->primaryKey;
131:         $displayField = $this->ScaffoldModel->displayField;
132:         $singularVar = Inflector::variable($modelClass);
133:         $pluralVar = Inflector::variable($this->controller->name);
134:         $singularHumanName = Inflector::humanize(Inflector::underscore($modelClass));
135:         $pluralHumanName = Inflector::humanize(Inflector::underscore($this->controller->name));
136:         $scaffoldFields = array_keys($this->ScaffoldModel->schema());
137:         $associations = $this->_associations();
138: 
139:         $this->controller->set(compact(
140:             'title_for_layout', 'modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar',
141:             'singularHumanName', 'pluralHumanName', 'scaffoldFields', 'associations'
142:         ));
143:         $this->controller->set('title_for_layout', $title);
144: 
145:         if ($this->controller->viewClass) {
146:             $this->controller->viewClass = 'Scaffold';
147:         }
148:         $this->_validSession = (
149:             isset($this->controller->Session) && $this->controller->Session->valid() != false
150:         );
151:         $this->_scaffold($request);
152:     }
153: 
154: /**
155:  * Renders a view action of scaffolded model.
156:  *
157:  * @param CakeRequest $request Request Object for scaffolding
158:  * @return mixed A rendered view of a row from Models database table
159:  * @throws NotFoundException
160:  */
161:     protected function _scaffoldView(CakeRequest $request) {
162:         if ($this->controller->beforeScaffold('view')) {
163:             if (isset($request->params['pass'][0])) {
164:                 $this->ScaffoldModel->id = $request->params['pass'][0];
165:             }
166:             if (!$this->ScaffoldModel->exists()) {
167:                 throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelKey)));
168:             }
169:             $this->ScaffoldModel->recursive = 1;
170:             $this->controller->request->data = $this->ScaffoldModel->read();
171:             $this->controller->set(
172:                 Inflector::variable($this->controller->modelClass), $this->request->data
173:             );
174:             $this->controller->render($this->request['action'], $this->layout);
175:         } elseif ($this->controller->scaffoldError('view') === false) {
176:             return $this->_scaffoldError();
177:         }
178:     }
179: 
180: /**
181:  * Renders index action of scaffolded model.
182:  *
183:  * @param array $params Parameters for scaffolding
184:  * @return mixed A rendered view listing rows from Models database table
185:  */
186:     protected function _scaffoldIndex($params) {
187:         if ($this->controller->beforeScaffold('index')) {
188:             $this->ScaffoldModel->recursive = 0;
189:             $this->controller->set(
190:                 Inflector::variable($this->controller->name), $this->controller->paginate()
191:             );
192:             $this->controller->render($this->request['action'], $this->layout);
193:         } elseif ($this->controller->scaffoldError('index') === false) {
194:             return $this->_scaffoldError();
195:         }
196:     }
197: 
198: /**
199:  * Renders an add or edit action for scaffolded model.
200:  *
201:  * @param string $action Action (add or edit)
202:  * @return mixed A rendered view with a form to edit or add a record in the Models database table
203:  */
204:     protected function _scaffoldForm($action = 'edit') {
205:         $this->controller->viewVars['scaffoldFields'] = array_merge(
206:             $this->controller->viewVars['scaffoldFields'],
207:             array_keys($this->ScaffoldModel->hasAndBelongsToMany)
208:         );
209:         $this->controller->render($action, $this->layout);
210:     }
211: 
212: /**
213:  * Saves or updates the scaffolded model.
214:  *
215:  * @param CakeRequest $request Request Object for scaffolding
216:  * @param string $action add or edit
217:  * @return mixed Success on save/update, add/edit form if data is empty or error if save or update fails
218:  * @throws NotFoundException
219:  */
220:     protected function _scaffoldSave(CakeRequest $request, $action = 'edit') {
221:         $formAction = 'edit';
222:         $success = __d('cake', 'updated');
223:         if ($action === 'add') {
224:             $formAction = 'add';
225:             $success = __d('cake', 'saved');
226:         }
227: 
228:         if ($this->controller->beforeScaffold($action)) {
229:             if ($action == 'edit') {
230:                 if (isset($request->params['pass'][0])) {
231:                     $this->ScaffoldModel->id = $request['pass'][0];
232:                 }
233:                 if (!$this->ScaffoldModel->exists()) {
234:                     throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelKey)));
235:                 }
236:             }
237: 
238:             if (!empty($request->data)) {
239:                 if ($action == 'create') {
240:                     $this->ScaffoldModel->create();
241:                 }
242: 
243:                 if ($this->ScaffoldModel->save($request->data)) {
244:                     if ($this->controller->afterScaffoldSave($action)) {
245:                         $message = __d('cake',
246:                             'The %1$s has been %2$s',
247:                             Inflector::humanize($this->modelKey),
248:                             $success
249:                         );
250:                         return $this->_sendMessage($message);
251:                     } else {
252:                         return $this->controller->afterScaffoldSaveError($action);
253:                     }
254:                 } else {
255:                     if ($this->_validSession) {
256:                         $this->controller->Session->setFlash(__d('cake', 'Please correct errors below.'));
257:                     }
258:                 }
259:             }
260: 
261:             if (empty($request->data)) {
262:                 if ($this->ScaffoldModel->id) {
263:                     $this->controller->data = $request->data = $this->ScaffoldModel->read();
264:                 } else {
265:                     $this->controller->data = $request->data = $this->ScaffoldModel->create();
266:                 }
267:             }
268: 
269:             foreach ($this->ScaffoldModel->belongsTo as $assocName => $assocData) {
270:                 $varName = Inflector::variable(Inflector::pluralize(
271:                     preg_replace('/(?:_id)$/', '', $assocData['foreignKey'])
272:                 ));
273:                 $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
274:             }
275:             foreach ($this->ScaffoldModel->hasAndBelongsToMany as $assocName => $assocData) {
276:                 $varName = Inflector::variable(Inflector::pluralize($assocName));
277:                 $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
278:             }
279: 
280:             return $this->_scaffoldForm($formAction);
281:         } elseif ($this->controller->scaffoldError($action) === false) {
282:             return $this->_scaffoldError();
283:         }
284:     }
285: 
286: /**
287:  * Performs a delete on given scaffolded Model.
288:  *
289:  * @param CakeRequest $request Request for scaffolding
290:  * @return mixed Success on delete, error if delete fails
291:  * @throws MethodNotAllowedException When HTTP method is not a DELETE
292:  * @throws NotFoundException When id being deleted does not exist.
293:  */
294:     protected function _scaffoldDelete(CakeRequest $request) {
295:         if ($this->controller->beforeScaffold('delete')) {
296:             if (!$request->is('post')) {
297:                 throw new MethodNotAllowedException();
298:             }
299:             $id = false;
300:             if (isset($request->params['pass'][0])) {
301:                 $id = $request->params['pass'][0];
302:             }
303:             $this->ScaffoldModel->id = $id;
304:             if (!$this->ScaffoldModel->exists()) {
305:                 throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelClass)));
306:             }
307:             if ($this->ScaffoldModel->delete()) {
308:                 $message = __d('cake', 'The %1$s with id: %2$s has been deleted.', Inflector::humanize($this->modelClass), $id);
309:                 return $this->_sendMessage($message);
310:             } else {
311:                 $message = __d('cake',
312:                     'There was an error deleting the %1$s with id: %2$s',
313:                     Inflector::humanize($this->modelClass),
314:                     $id
315:                 );
316:                 return $this->_sendMessage($message);
317:             }
318:         } elseif ($this->controller->scaffoldError('delete') === false) {
319:             return $this->_scaffoldError();
320:         }
321:     }
322: 
323: /**
324:  * Sends a message to the user.  Either uses Sessions or flash messages depending
325:  * on the availability of a session
326:  *
327:  * @param string $message Message to display
328:  * @return void
329:  */
330:     protected function _sendMessage($message) {
331:         if ($this->_validSession) {
332:             $this->controller->Session->setFlash($message);
333:             $this->controller->redirect($this->redirect);
334:         } else {
335:             $this->controller->flash($message, $this->redirect);
336:         }
337:     }
338: 
339: /**
340:  * Show a scaffold error
341:  *
342:  * @return mixed A rendered view showing the error
343:  */
344:     protected function _scaffoldError() {
345:         return $this->controller->render('error', $this->layout);
346:     }
347: 
348: /**
349:  * When methods are now present in a controller
350:  * scaffoldView is used to call default Scaffold methods if:
351:  * `public $scaffold;` is placed in the controller's class definition.
352:  *
353:  * @param CakeRequest $request Request object for scaffolding
354:  * @return mixed A rendered view of scaffold action, or showing the error
355:  * @throws MissingActionException When methods are not scaffolded.
356:  * @throws MissingDatabaseException When the database connection is undefined.
357:  */
358:     protected function _scaffold(CakeRequest $request) {
359:         $db = ConnectionManager::getDataSource($this->ScaffoldModel->useDbConfig);
360:         $prefixes = Configure::read('Routing.prefixes');
361:         $scaffoldPrefix = $this->scaffoldActions;
362: 
363:         if (isset($db)) {
364:             if (empty($this->scaffoldActions)) {
365:                 $this->scaffoldActions = array(
366:                     'index', 'list', 'view', 'add', 'create', 'edit', 'update', 'delete'
367:                 );
368:             } elseif (!empty($prefixes) && in_array($scaffoldPrefix, $prefixes)) {
369:                 $this->scaffoldActions = array(
370:                     $scaffoldPrefix . '_index',
371:                     $scaffoldPrefix . '_list',
372:                     $scaffoldPrefix . '_view',
373:                     $scaffoldPrefix . '_add',
374:                     $scaffoldPrefix . '_create',
375:                     $scaffoldPrefix . '_edit',
376:                     $scaffoldPrefix . '_update',
377:                     $scaffoldPrefix . '_delete'
378:                 );
379:             }
380: 
381:             if (in_array($request->params['action'], $this->scaffoldActions)) {
382:                 if (!empty($prefixes)) {
383:                     $request->params['action'] = str_replace($scaffoldPrefix . '_', '', $request->params['action']);
384:                 }
385:                 switch ($request->params['action']) {
386:                     case 'index':
387:                     case 'list':
388:                         $this->_scaffoldIndex($request);
389:                     break;
390:                     case 'view':
391:                         $this->_scaffoldView($request);
392:                     break;
393:                     case 'add':
394:                     case 'create':
395:                         $this->_scaffoldSave($request, 'add');
396:                     break;
397:                     case 'edit':
398:                     case 'update':
399:                         $this->_scaffoldSave($request, 'edit');
400:                     break;
401:                     case 'delete':
402:                         $this->_scaffoldDelete($request);
403:                     break;
404:                 }
405:             } else {
406:                 throw new MissingActionException(array(
407:                     'controller' => $this->controller->name,
408:                     'action' => $request->action
409:                 ));
410:             }
411:         } else {
412:             throw new MissingDatabaseException(array('connection' => $this->ScaffoldModel->useDbConfig));
413:         }
414:     }
415: 
416: /**
417:  * Returns associations for controllers models.
418:  *
419:  * @return array Associations for model
420:  */
421:     protected function _associations() {
422:         $keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
423:         $associations = array();
424: 
425:         foreach ($keys as $key => $type) {
426:             foreach ($this->ScaffoldModel->{$type} as $assocKey => $assocData) {
427:                 $associations[$type][$assocKey]['primaryKey'] =
428:                     $this->ScaffoldModel->{$assocKey}->primaryKey;
429: 
430:                 $associations[$type][$assocKey]['displayField'] =
431:                     $this->ScaffoldModel->{$assocKey}->displayField;
432: 
433:                 $associations[$type][$assocKey]['foreignKey'] =
434:                     $assocData['foreignKey'];
435: 
436:                 list($plugin, $model) = pluginSplit($assocData['className']);
437:                 if ($plugin) {
438:                     $plugin = Inflector::underscore($plugin);
439:                 }
440:                 $associations[$type][$assocKey]['plugin'] = $plugin;
441: 
442:                 $associations[$type][$assocKey]['controller'] =
443:                     Inflector::pluralize(Inflector::underscore($model));
444: 
445:                 if ($type == 'hasAndBelongsToMany') {
446:                     $associations[$type][$assocKey]['with'] = $assocData['with'];
447:                 }
448:             }
449:         }
450:         return $associations;
451:     }
452: 
453: }
454: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs