Using one model to read and another to save data - cakephp

I have a model named google_news.php which uses the external data, and another model saved_news.php which uses my saved_news table in database,
In my controller I declared that Im using this two models:
var $uses = array('GoogleNews', 'SavedNews');
and my index function reads data:
$this->set('news',$this->GoogleNews->find('all'));
and my view looks like this:
<?php foreach( $news as $newsItem ) : ?>
<?php echo $html->link($newsItem['GoogleNews']['title'], array('action'=>'add', $newsItem['GoogleNews']['title'])); ?>
<?php echo $newsItem['GoogleNews']['encoded']; ?>
<em>
<hr>
<?php endforeach; ?>
How to write the add function in my controller to save each data to my database?

You should assign what you need to be saved into $this->data['ModelName'] as array of fields. Take a look at saving data in the book. That will explain more about the formatting that needs to be followed.

Related

cakephp getLastInsertId not working

Here I have used cakephp js helper to send data,After insert One data I need this last id for farther work.Here I have tried bellow code in Addcontroller
if ($this->Patient->save($this->request->data)) {
$lastid=$this->Patient->getLastInsertId();
$patient=$this->Patient->find('all',array(
'conditions'=>array('Patient.id'=>$lastid ),
'recursive' => -1
));
$this->set('patient', $patient);
}
In add.ctp I have tried bellow code but I haven't get last id here.
<?php foreach ($patient as $patient): ?>
<?php echo h($patient['Patient']['id']); ?>
<?php endforeach; ?>
Method getLastInsertId() return id of just saved records.
If you need this id in your view just after save, you must first set that variable in your controller like $this->set(compact('lastid','patient'); and then use in view <?php echo $lastid; ?>
use
if ($this->Patient->save($this->request->data)) {
$id = $this->Patient->id;
$patient=$this->Patient->find('all',array('conditions'=>array('Patient.id'=>$id),'recursive' => -1));
$this->set->('patient', $patient);
//If you are saving the record with ajax which it looks like you
//might be from your question you will need the following instead
//of $this->set->('patient', $patient); try:
return json_encode($patient);
You will then also need to update your js ajax call, you will have a json array to decode so parse it with jquery and append it back into your view.
Cake will always give you the id of record you have just saved, by simply adding $id = $this->MyModel->id; You can use the id to query for the record.
Try below code:
In controller:
$lastid=$this->Patient->getLastInsertId();
$this->set(compact('lastid','patient');
Then use $lastid in View file.
In controller:
$lastid=$this->Patient->getLastInsertId();
$patient['Patient']['last_id'] = $lastid;
then use $patient['Patient']['last_id'] in your view file.

How do i use batch insert in yii

Please i need a big help here. AM developing a yii application where i have to loop through my form and do a batch insert. I found bacth update in yii but ive not been able to see how to do batch insert and validation. Please help.
Here is my View:
<?php for($i=0;$i< $this->getDisplayArchModel();$i++) {?>
<FIELDSET class="radios">
<div class="row">
<?php echo $form->labelEx($model,'competency_type'); ?>
<?php echo $form->textField($model,'competency_type'); ?>
<?php echo $form->error($model,'competency_type'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'definition'); ?>
<?php echo $form->textArea($model,'definition'); ?>
<?php echo $form->error($model,'definition'); ?>
</div>
</fieldset>
<?php } ?>
Controller ::
public function actionDisplayArchModel()
{
$validateCat = $this->getDisplayArchModel();
if($validateCat == NULL)
$this->redirect(array('architecture'));
$model = new CompetencyType;
if(isset($_POST['CompetencyType']))
{
$model->attributes = $_POST['CompetencyType'];
if($model->validate()){
foreach($_POST['CompetencyType'] as $value)
{
echo $value;
}
}
}
$this->render('displayArchModel' ,array('model'=>$model));
}
Sometimes we want to collect user input in a batch mode. That is, the user can enter the information for multiple model instances and submit them all at once. We call this tabular input because the input fields are often presented in an HTML table.
To work with tabular input, we first need to create or populate an array of model instances, depending on whether we are inserting or updating the data. We then retrieve the user input data from the $_POST variable and assign it to each model. A slight difference from single model input is that we retrieve the input data using $_POST['ModelClass'][$i] instead of $_POST['ModelClass'].
more on collecting Tabular Input

CakePHP How to echo just one record

I want to echo a full name from my MySQL database in my header. When that name is clicked in a list it filters all the records and displays all the records related to that name only. I managed to get the filter working, but not able to display the name in header.
<? $this->read('$jobs as $row'); ?>
<h1><?=$row['Employee']['first_name']?> <?=$row['Employee']['last_name']?>'s Jobs</h1>
<? $this->end(); ?>
If I'm not wrong, you are trying to retrieve this array, I'm assuing $jobs contains single row.
try this
<?php
if (isset($jobs)) {
foreach($jobs as $row){
if (isset($row['Employee']['last_name']))
$last = $row['Employee']['last_name'];
$first = 'N/A';
if (isset($row['Employee']['first_name']))
$first = $row['Employee']['first_name'];
?>
<h1><?php echo $first.' '. $last?>'s Jobs</h1>
<?php } }?>
OR
<h1><?php isset($jobs[0]['Employee']['first_name']) ? $jobs[0]['Employee']['first_name'] : 'N/A' .' '. isset($jobs[0]['Employee']['last_name']) ? $jobs[0]['Employee']['last_name'] : 'N/A'?>'s Jobs</h1>
This can be much more easily achieved through the use of virtual fields. The example in the Cake book is practically identical to your needs.
Just add this to your Employee model:
public $virtualFields = array(
'full_name' => 'CONCAT(Employee.first_name, " ", Employee.last_name)'
);
Now [Employee]['full_name'] can be used without having to use any logic.
Here's the link to the Cake book page covering virtual fields: http://book.cakephp.org/2.0/en/models/virtual-fields.html

Magento product display text "array" instead of multiple values

On my Magento product page; when a product has multiple values for one custom attribute; instead of displaying the values it displays the text "array". It works fine with one value.
Thanks,
-Sam
You can do something like:
<?php
foreach($_product->getMetal() as $name => $value): ?>
<?php echo $name;?> = <?php echo $value;?>
<?php
endforeach; ?>
Magento takes advantage of PHP's magic getter/setter functionality (http://www.php.net/manual/en/language.oop5.overloading.php#object.get).
You can do a vardump($_product) to see the available attributes (they are stored in the _data array in the product). Then to retrieve one of them, you just remove the underscores and change the first letter of each word to uppercase.
EDIT:
If the above code doesn't output values, you can do this (which will tell you how to get to the value):
<?php
foreach($_product->getMetal() as $attribute): ?>
<?php var_dump($attribute); ?>
<?php
endforeach; ?>
I found this on Magento forums and it seems to work:
` getData('attribute_name')): ?>
getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product)) ?>
`

how to access a function defined in app_controller from a ctp

I have function in app_controller.php.The function is like:
function globalSum($Var1,$Var2)
{
$Var3 = $Var1 + $Var2;
return $Var3;
}
Now I want to access this function from any CTP file to get the value after sum.when I call this function the arments will be send from the ctp file.
So,anybody can tell me how to call this function with arguments from the ctp file??
Thanks in advance..
The way you're trying to do this probably isn't the best, seeing as it's working against the MVC architecture that CakePHP uses.
In MVC, the ctp file is your view and should only act as a template, to the greatest extent possible, with any values that you need in the view should be passed to it from the controller.
You have a number of simple solutions to your problem.
One is simply to do the addition in the view:
index.ctp
<?php
echo $var1 + $var2
?>
For such a simple operation, why bother with a separate function?
If your function is more complicated, you can put it in the AppController and then set the view variable in the controller that the action belongs to. For example:
app_controller.php
<?php
function globalSum($Var1,$Var2) {
$Var3 = $Var1 + $Var2;
return $Var3;
}
?>
posts_controller.php
<?php
function index() {
$this->set('var3', $this->globalSum($var1,$var2));
}
?>
index.ctp
<?php
echo $var3;
?>
Hope that helps.

Resources