CakePHP SQL Error when trying to use loadModel - mysql

I have two controllers (Feeds & Items), on the Items add view, I want some information from the Feeds table visible to the user.
So in the Items Controller I'm trying to access the Feed model, everything I have come across recommends to use loadModel but when I try to load the Items add view, I getting the following error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
SQL Query: SHOW TABLES FROM
Please note there is no relationship setup between these two tables.
Feed Model
App::uses('AppModel', 'Model');
class Feed extends AppModel {}
Item Model
App::uses('AppModel', 'Model');
class Item extends AppModel {}
Item Controller
App::uses('Feed','Model');
class ItemsController extends DirectResponseAppController {
function add() {
if ($this->request->is('post')) {
$this->Item->create();
if ($this->Item->save($this->request->data)) {
$this->Session->setFlash(__('Your item has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your item.'));
} else {
$this->loadModel('Feed');
$xxx = $this->Feed->find('all');
}
}
}
Any help appreciated.
** EDIT **
After a bit more digging, the problem seems to be with this line:
$xxx = $this->Feed->find('all');
I'm just not sure why.

If your Feed is inside a plugin (DirectResponse), you must use loadModel like this:
$this->loadModel('DirectResponse.Feed');

Related

Eloquent model cannot select from MySQL view

Has MySQL view 'histstat'.
In MySQL select * from histstat works fine.
Laravel model is very simple:
class HistStat extends Model
{
use HasFactory;
protected $table = 'histstat';
protected $fillable = ['day', 'total', 'paid'];
}
Then I want to get first 14 records of hisstat:
$dynamic = HistStat::all()->slice(14);
... and execution ends with error SQLSTATE[42000]: Syntax error or access violation: 1055 (SQL: select * from 'histstat')
When I try to use table-based model ($dynamic = History::all()->slice(14);) - everything works fine.
So, the problem in MySQL view + Laravel.
How to use view-based model in Laravel ?
For the SQL error,
In config\database.php in "mysql" array
Set 'strict' => false
I think this one will help Laravel: Syntax error or access violation: 1055 Error.
For the first 14 records, you can do it
HistStat::oldest()->take(14)->get();
better than all() because I do not recommend using it if you have a huge table that will be load on the server because all() get all records select * from "users"
In config/datatable.php (mysql section):
Change strict to false
Add / change in options - PDO::ATTR_EMULATE_PREPARES => true

Can't delete a collection of Laravel eloquent

I am retrieving a collection from db and want to delete it. This is the code.
$signal_id = $request->del_signal_id;
$signal_details = VwDistressSignals::where('signal_id', $signal_id)->delete();
return "Success";
}
And this is the error.
message: "SQLSTATE[HY000]: General error: 1395 Can not delete from join view 'dvp.vw_distresssignals' (SQL: delete from `vw_distresssignals` where `signal_id` = 2)"
I have also tried giving all the column names. This is the model...
<?php
namespace App\Models;
use Reliese\Database\Eloquent\Model as Eloquent;
class VwDistressSignals extends Eloquent
{
protected $table = 'vw_distresssignals';
}
This error is happening because your relationship is not properly defined.
Delete with the loop
$signal_id = $request->del_signal_id;
$signal_details = VwDistressSignals::where('signal_id', $signal_id)->get();
foreach($signal_details as $detail)
{
$detail->delete();
}
return "Success";
Since you haven't shared your model, I assume it corresponds to a database view rather than a table.
If so try deleting from the base table(s) as join views are not deletable.
DELETE ... Join views are not allowed.
https://dev.mysql.com/doc/refman/5.7/en/view-updatability.html

What is the usage of observer.php in a module of magento

Actually I working now in magento for developing a module to check the voucher code used or not. The details are stored in a new table. In my config.xml, I specified the observer page for fetching the details from db table. But I don't know the exact use of observer page in magento. Can I use observer page for this usage.
But it proceed to an error
I checked the log file
which is
a:5:{i:0;s:203:"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=' at line 1";i:1;s:1677:"#0 C:\wamp\www\Mymagento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
My observer.php file is also shown below
class Module_Voucher_Model_Observer
{
public function __contruct()
{
$coupon_code = trim(Mage::getSingleton("core/session")->getData("coupon_code"));
}
public function getresultofVoucher($coupon_code)
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$table = "voucher_code_status_table";
$query = 'SELECT * FROM ' . $table. 'WHERE value='.$coupon_code;
$results = $readConnection->fetchAll($query);
return $results;
}
}
Please help what is the mysql error here. Please help as soon as possible
Thanks in advance
Observer.php is a model class file, like all models this also can be called any were we need its function.
Normally we use observers when using magento events. In config.xml we declare events and we use observer functions to handle the event when it occurs.
I have gone through your error and code. It seems the code doesn't get the value of coupon code. Please check whether there is any value coming in $coupon_code.
That may be the issue.
Thanks.
An observer is an EventListener, events are dispatched in Magento with:
Mage::dispatchEvent('event_name', array('item' => $this));
When an event is dispatched, Magento will check which observers are bound to it, and will call the function defined in the config with a Varien_Event_Observer object as its parameter.
You're function could be something like this:
public function getresultofVoucher(Varien_Event_Observer $observer)
{
$item = $observer->getItem();
// do something with it
}

CakeDC Search plugin doesn't work

i'm trying to develop a simple search form using cakedc plugin, i followed step by step the instructions , but i got the next error:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'validateSearch' at line 1
SQL Query: validateSearch
i don't know what i got wrong, can you help me?, this is what i've got, Thank you.
In the Controller:
class ProductosController extends AppController {
public $name = 'Productos';
public $uses = array('Empleado','Cliente', 'Mesa','Producto', 'Productotipo','Productos');
public $components = array('Search.Prg');
public $presetVars = true;
public $paginate=array();
public function ventas(){
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Producto->parseCriteria($this->Prg->parsedParams());
$this->set('productos', $this->paginate());
}
In the Model:
class Producto extends AppModel {
public $name = 'Producto';
public $displayField = 'productotipos_id';
public $belongsTo = array('Productotipo'=>array('className'=>'Productotipo','foreignKey'=>'productotipos_id','conditions'=>'','order'=>''));
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'nombre' => array('type' => 'like')
);
In the view
<?php echo $this->Form->create('Producto', array('url' => array_merge(array('action'=>'ventas'), $this->params['pass'])));?>
<fieldset>
<legend>Pedido</legend>
<?php
echo $this->Form->input('Producto.nombre', array('div'=>false, 'empty'=>true));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
</fieldset>
Try to reorder $uses that Controller's model will be the first:
public $uses = array('Producto', 'Empleado', 'Cliente', 'Mesa', 'Productotipo');
Should help. Don't know why, but probably some of methods from CakeDC Search plugin depends on first item in this array.
SQL Query: validateSearch
Is the usual error when you try to call a model method that does not exist.
So whatever model you try to paginate (it was not a good idea to not paste the class declarations...) does not use the searchable behavior for some reason.
In the case the model is the correct one the behavior is not loaded for some reason that you'll have to figure out.

CakePHP 2.4.2 Why is Cake using model names as SQL queries?

I have a fresh out o' the git repo CakePHP application. I just made a new model (Ingests) to track what data our system brings in. I wanted to make functions start() and end(), but end is protected so I switched to begin() and finish().
No matter what I do, CakePHP is trying to execute the model function names verbatim as the SQL queries. I have another model in this app that I've been working on this week that has not had this issue at all. Creating a new table/model today is when the problem appeared.
IngestsController.php
public function test(){
$this->autoRender = false;
//$result = $this->Ingest->finish();
$result = $this->Ingest->xyz();
debug($result);
}
Ingests.php Model
public function finish($id){
return 'giraffe';
}
public function xyz(){
return 'abc';
}
Output:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'xyz' at line 1
SQL Query: xyz
I tried xyz() because there is no way xyz() could be a protected/not allowed function name... but apparently it's just as bad a choice as finish(). If I run the finish() function, I get the same output... "SQL Query: finish"
Check your file name, the name for your file name model should be Ingest.php instead of Ingests.php also check your class declaration:
<?php
App::uses('AppModel', 'Model');
class Ingest extends AppModel { //Make sure the model name is singular
public function finish($id){
return 'giraffe';
}
public function xyz(){
return 'abc';
}
}