I have a table in my database called modules so in my controller I write like this to retrieve datas from the table
$allModuleNames = DB::table('users')->get();
return view("BaseView.home")->with('$allModuleNames',$allModuleNames);
But for some weird reason I am getting syntax error, unexpected ';' error on the query like. This must be silly I guess,not able to pass through this. Can someone help?
I have added this to the header
use Illuminate\Support\Facades\DB;
And when I hover over table I get method table not found in \illuminate\Support\Faces\DB
Use modules intead of users :
$allModuleNames = DB::table('modules')->get();
and you dont need the $ on this line :
return view("BaseView.home")->with('allModuleNames',$allModuleNames);
Related
Trying to retrieve rows which has same value(関西).
First I created a blade for this (kansai.blade)
And then I set the route:
Route::get('/kansai', 'PagesController#kansai');
I set the controller:
public function kansai()
{
$estates = allestates::where('region', '=', '関西')->get();
return view('pages.kansai', compact('estates'));
}
After that gave the link in main.blade:
<li>関西</li>
But it returns with an error:
Trying to get property of non-object (View:
/var/www/html/laravel/resources/views/welcome.blade.php)
Do I missing something here? The problem is my controller I guess?
Any idea? Thank you.
estates is an array not an object in this context. either loop trough it or specify an index.
I solved the problem, it's likely my mistake actually.
I already retrieved the data in kansai.blade.
So I just the pass the link in main.blade
which is like below.
<li>関西</li>
and with this problem solved.
I'm having an issue calling an custom defined function in my database from Doctrine when using the Raw SQL.
Here is an example SQL Query that is being run
SELECT unicodeDecoder(answer) from answers;
unicodeDecoder is a custom defined function in my database and runs perfectly fine when I run the SQL statement directly on the database.
However when I run the query using raw sql, as follows:
$sql = "SELECT unicodeDecoder(answer) from answers";
$stmt = $this->getEntitityManager()->getConnection()->prepare($sql);
$stmt->execute();
I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION unicodeDecoder does not exist
Do I need to create a custom ORM mapping when using custom functions or could this be some form of caching issue?
Any help would be massively appreciated.
Thanks in advance
Try to prefix the function with database name:
$sql = "SELECT my_db.unicodeDecoder(answer) from answers";
If found the issue. Thank you to #alex-blex for giving me a point of somewhere to start looking.
My database has a . in the name: cms.app.com
The issue was caused by the .
When Doctrine tries to access the method it automatically appends the database name to the beginning.
When I attempted to add the cms.app.com to the beginning (as suggested by #alex-blex) it created the following method call (which is the wrong syntax)
cms.app.com.unicodeDecoder()
I attempted the following
`cms.app.com`.unicodeDecoder but this still resolved to
cms.app.com.unicodeDecoder()
I removed the . from the database name and it now works as expected.
Important Note: Do not use . in your database name as Doctorine doesn't resolve the name correctly as the ` symbol get stripped out.
I'm upgrading a big project form Yii1 to Yii2. I'm having some problems regarding to ORM.
I have several relation declared in the following fashion(basically a copy-paste from the guidebook):
class Order extends \yii\db\ActiveRecord {
/* other code */
public function getAffiliate()
{
return $this->hasOne(Affiliate::className(), ['id_affiliate' => 'affiliate_id']);
}
Whenever I try to echo or w/e $order->affiliate->name; I get the following error:
yii\base\ErrorException: Trying to get property of non-object
I've got no experience with Yii1 what so ever. Something weird about this project is the database. All tables start with yii_tablename and id's are: id_tablename. Was that normal for Yii1 and could this be causing the issue above?
Edit: When I execute the function like so: $order->getAffilate() it returns an ActiveQuery WITHOUT the data from the affiliate.
When I execute the following:
$order->getBillingAddress()->one();
I get a weird error:
Getting unknown property: app\models\Order::billing
return $this->hasOne(Affiliate::className(), ['id_affiliate' => 'affiliate_id']);
It's mean that when you call $order->affiliate yii2 will find in Affiliate table on id_affiliate field current Order affiliate_id value and selected one value.
Check that you have right field names and database have right data.
When you call $order->affiliate you will get Affiliate object. But if you call $order->getAffiliate() you will get ActiveQuery object.
I found a solution. One which I don't really like though, but it does the job. Was reading this thread: link.
Kartik V
The problem is clearly in uniqueness in naming your relation and your model attribute. In your User model, you have an attribute named role and you also have a relation getter named getRole.
So I changed the name of the getter like so:
public function getOrderAffiliate()
{
return $this->hasOne(Affiliate::className(), ['id_affiliate' => 'affiliate_id']);
}
And that fixed the issue. Never had this issue before and wonder why this happened though.
I'm trying to implement APYDataGridBundle on Symfony, with SQL Server. Symfony throws in this exception:
SQLSTATE[HY000]: General error: 105 General SQL Server error: Check messages from the SQL Server [105] (severity 15) [SELECT TOP 50 [0_.PI_PK AS PI_PK0, [0_.ProductName AS ProductName1, [0_.ProductDetails AS ProductDetails2 FROM [TSOFT_LEARN].[dbo].[tblProductDemo] [0_]
I tried:
$repo = $em->getRepository("ProductOrderLookupBundle:Product");
$product = $repo->findAll();
and everything worked fine, but it breaks for over 1 million records. Someone suggested to me to use APYDatagridBundle like here. I have tried ThraceDataGrid Bundle before and it gave me this same problem.
If I remove the "[0_" and everything worked fine while running the query on SQL Server.
Can anybody tell me what might be the problem?
Doctrine always create alias for tables in the generated queries. I always had the same kind of queries, i don't know if it can be disabled or not but better not because i don't know how it will react for joins , what you can do is to add a function in your repository :
public function getAllProducts(){
$qb = $this->_em->createQueryBuilder()
->select('partial p.{ /*add all fields needed separated by coma optimized for big queries get only whats needed */ }')
->from('Product', 'p');
$results = $qb->getQuery()->getResult();
return $results;
}
**SOLVED:**The problem was with how I defined my table names in the doctrine entities. I gave the name
[TSOFT_LEARN].[dbo].[tblOrders]
and it should be
TSOFT_LEARN.dbo.tblOrders
Because of that my alias was becoming [0_ instead of t0_. I suppose its how doctrine creates its aliases by taking the first character of the table name. Since the table name was starting with "[" the alias got converted to [0_.
My plan is to create a temporary table using the $this->Model->query(); method then load it as a Model but I'm getting an error staying "Missing Database Table". Turning on debug to level two shows that the temporary table is success fully created but for some reason, when I try and load it as a model it doesn't work. It looks like cake doesn't even try to see if the table exists as there is no "SHOW FULL COLUMNS FROM ...' query being displayed. Not sure how to force cake to check for its existence.
$tmpModel = 'tempModel';
$tmpTable = 'temp_models';
$this->Model->query('CREATE TEMPORARY TABLE `'.$tmpTable ... );
$this->loadModel($tmpModel);
Thanks in advance.
$tmpModel = 'TempModel'; // CamelCase
also try, ClassRegisty::init($tmpModel);
final issue may be cache. but dont think so
Ok, after looking around a bit I have found a solution that works.
The problem was that Cake had already loaded the models caches at page load so used them as reference to the existence of a table. To resolve this problem I used "App::import('Model', $tmpModel);" which created the new modelCache allowing the loadModel script to run successfully.
$tmpModel = 'tempModel';
$tmpTable = 'temp_models';
$this->Model->query('CREATE TEMPORARY TABLE `'.$tmpTable ... );
App::import('Model', $tmpModel);
$this->loadModel($tmpModel);
Thanks anyway
After doing google for an hour .. finally found a way to have Model on Temporary table and it works like a charm
http://web2.0goodies.com/blog/uncategorized/mysql-temporary-tables-and-cakephp-1-3/