Monday, October 3, 2011

Node and attachment forms on single page

I have very simple Nodes model/view/controller which by default stores
information like Title, Body... To each Node I need attached images,
or other files. So I created a Uploads M/V/C with the attachable
behavior from phpvideotutorials: http://pastebin.com/87pQtgib
Now if I use the Uploads M/V/C I am able to upload images and store
some of image information in the 'uploads' table, but I need to set
the attachment form into the edit nodes pages and use the 'nodes'
table for the uploads.
So I read the information for multiple forms in same page:
http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model
I created:
models/add_attachment.php
-------------------------------------------
<?php
class AddAttachement extends Node {
var $actsAs = array('Attachable' => array( 'types' => array('image/
jpeg' => 'jpg', 'image/pjpeg' => 'jpg', 'application/zip' => 'zip')));
}
?>

views/nodes/admin_edit.ctp:
-----------------------------------------
<div class="nodes form">
<?php echo $this->Form->create('Node');?>
<fieldset>
<legend><?php __('Admin Edit Node'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('title');
echo $this->Form->input('body');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
<?php echo $this->Form->create('AddAttachment', array('url' => '/
admin/nodes/add'));
echo $this->Form->input('attachment', array('type' => 'file'));
echo $this->Form->end('Upload');?>
</div>

controllers/nodes_controller.php
----------------------------------------------
..................
function admin_add() {
if (!empty($this->data)) {
$this->loadModel('AddAttachment');
if($this->data['AddAttachment']) {
$this->AddAttachment->create();
if ($this->AddAttachment->save($this->data)) {
$this->Session->setFlash(__('The node has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The node could not be saved. Please,
try again.', true));
}
} else {
$this->Node->create();
if ($this->Node->save($this->data)) {
$this->Session->setFlash(__('The node has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The node could not be saved. Please,
try again.', true));
}
}


}
}
......................
(i pasted only the modified part)

As a result I get an error message when trying to upload a file:
--------------------------------------------------------------------------------------------------
Notice (8): Undefined index: Node [APP/models/behaviors/
attachable.php, line 38]
Code | Context

{

extract($this->settings[$model->name]);

$attachment = $model->data[$model->name][$label];

$model = AddAttachment
AddAttachment::$actsAs = array
AddAttachment::$name = "Node"
AddAttachment::$displayField = "title"
AddAttachment::$useDbConfig = "default"
AddAttachment::$useTable = "nodes"
AddAttachment::$id = false
AddAttachment::$data = array
AddAttachment::$table = "nodes"
AddAttachment::$primaryKey = "id"
AddAttachment::$_schema = array
AddAttachment::$validate = array
AddAttachment::$validationErrors = array
AddAttachment::$tablePrefix = ""
AddAttachment::$alias = "AddAttachment"
AddAttachment::$tableToModel = array
AddAttachment::$logTransactions = false
AddAttachment::$cacheQueries = false
AddAttachment::$belongsTo = array
AddAttachment::$hasOne = array
AddAttachment::$hasMany = array
AddAttachment::$hasAndBelongsToMany = array
AddAttachment::$Behaviors = BehaviorCollection object
AddAttachment::$whitelist = array
AddAttachment::$cacheSources = true
AddAttachment::$findQueryType = NULL
AddAttachment::$recursive = 1
AddAttachment::$order = NULL
AddAttachment::$virtualFields = array
AddAttachment::$__associationKeys = array
AddAttachment::$__associations = array
AddAttachment::$__backAssociation = array
AddAttachment::$__insertID = NULL
AddAttachment::$__numRows = NULL
AddAttachment::$__affectedRows = NULL
AddAttachment::$_findMethods = array

And I am not able to attach anything. Just for testing I tried to
change the file input form with simple title and body fields, and to
remove the Attachable behavior form the AddAttachment behavior

Sorry for the long post, trying to explain everything. Thanks to
anyone able to help

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: