Thank you for the quick reply, I appreciate it.
I've tried what you suggested, and it is working, up to a point. Here
is the code I'm using:
DataSource - app/plugins/array_datasource/models/datasources/
array_source.php
(I've only included the relevant modifications instead of the entire
file, unless needed)
function __construct($config = array()) {
parent::__construct($config);
//Modification to read the XML file and populate the
$records array:
App::import('Core', 'Xml');
App::import('Core', 'File');
$this->FileUtil =& new File(WWW_ROOT.'files/'.'data.xml');
$this->File = $this->FileUtil->read();
$xml = new Xml($this->File);
$this->records = $xml->toArray();
}
Controller - app/controllers/registros_controller.php
class RegistrosController extends AppController {
var $helpers = array ('Html','Form');
var $name = 'Registros';
function index() {
$this->set('registros', $this->Registro->find('all'));
//My guess is that find isn't working correctly
}
}
Model - app/models/registro.php
class Registro extends AppModel {
var $name = 'Registro';
var $useTables = false;
var $useDbConfig = 'local';
var $displayField = 'espanol_comun';
//Note that I've copied the same code from the datasource
here. It apparently makes no difference, since I've commented them
alternatively.
/*
function __construct($config = array()) {
parent::__construct($config);
//Modification to read the XML file and populate the
$records array:
App::import('Core', 'Xml');
App::import('Core', 'File');
$this->FileUtil =& new File(WWW_ROOT.'files/'.'data.xml');
$this->File = $this->FileUtil->read();
$xml = new Xml($this->File);
$this->records = $xml->toArray();
debug($this->records); //This works.
}*/
var $validate = array( 'espanol_comun' => array('rule' =>
'notEmpty'),
'categoria' => array('rule' =>
'notEmpty'),
'definicion' => array('rule' =>
'notEmpty'),
'ambito' => array('rule' =>
'notEmpty')
);
}
View - adapted from the Blog tutorial - app/views/registros/index.ctp
//Relevant foreach loop:
<?php foreach ($registros as $registro): ?>
<tr>
<td>
<?php echo $this->Html->link( $registro['Registro']
['espanol_comun'],
array('controller' =>
'registros',
'action' => 'view',
$registro['Registro']
['espanol_comun']));
?>
...
<?php endforeach; ?>
When I display the view, I get notice errors in each field, saying
"Notice (8): Undefined index: Registro [APP\views\registros\index.ctp,
line 16]" which corresponds to the foreach loop.
Any ideas on what I'm doing wrong? If I debug $this->records from the
__construct function, I get the array just fine.
(I don't know if it's a better idea to use pastebin to link the entire
code. I would be happy to do so if necessary.)
On 12 feb, 13:51, Ryan Schmidt <[email protected]> wrote:
> On Feb 11, 2011, at 20:34, Carlos Paparoni wrote:
>
>
>
> > To make an update on the situation:
>
> > I made the code work with the Array DataSource up to a point. The
> > problem right now is that it uses a $records array which is assigned
> > statically in the Model, like this:
> > <?php
> > class State extends AppModel {
> > var $name = 'State';
> > var $useDbConfig = 'local';
> > var $displayField = 'name';
> > //Here is where records are assigned to the array:
> > var $records = array(
> > array('id' => 1, 'name' => 'Alabama', 'abbr' => 'AL'),
> > array('id' => 2, 'name' => 'Alaska', 'abbr' => 'AK'),
> > array('id' => 3, 'name' => 'Arizona', 'abbr' => 'AZ')
> > );
> > }
> > ?>
>
> > The issue is that if I try to use a function inside the model (for
> > example, one I found to read a XML file and turn it into an array), I
> > get parsing errors from PHP.
> > So, I'm stuck. Where should I add that functionality? Should I edit
> > the datasource and have it read the XML from there?
>
> Correct, if you want to call PHP functions, you cannot do so in the
> declaration of your class; you must do so for example in the constructor:
>
> <?php
> class State extends AppModel {
> var $name = 'State';
> var $useDbConfig = 'local';
> var $displayField = 'name';
> function __construct() {
> parent::__construct();
> // here you could populate $this->records using a function
> instead
> $this->records = array(
> array('id' => 1, 'name' => 'Alabama', 'abbr' => 'AL'),
> array('id' => 2, 'name' => 'Alaska', 'abbr' => 'AK'),
> array('id' => 3, 'name' => 'Arizona', 'abbr' => 'AZ')
> );
> }
>
> }
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php