Doctrine Statement: Select from Database with uuid (value isnt readable) - doctrine

in Symfony i need access to another Database from another Project. So i created another entityManager for this connection.
Now i want read some Data where id field is uuid.
the statement looks like this:
$entityManager = $this->doctrine->getManager('myProject');
$connection = $entityManager->getConnection();
$statement = $connection->prepare(
'SELECT * FROM salutation '
);
$resultSet = $statement->executeQuery();
dd($resultSet->fetchAll());
But the id value is not readable.
"id" => b"\x0E2#\x05P\x08Gó€dˆk§\vè\x05"
Is there a way to tell Doctrine to handle this field as Uuid?
Thanks for your Help.

Related

How to query all posts by tag_id in Postgres ManyToMany Relationship in spring JPA repository?

How to query all posts by tag_id in Postgres ManyToMany relationship in Spring JPA repository?
These are the tables:
post
post_tag
tag
id
id_post
id
..
id_tag
..
If I query the PostgreSQL directly via pg_admin Query tool the following sql is working:
SELECT * FROM post WHERE ID IN (
select post_id from post_tag tag WHERE (tag_id = '12'))
I like to have this query in my JPA Repository, but the following isn't working.
#Query(value = "SELECT * FROM post WHERE ID IN (select post_id from
post_tag tag WHERE (tag_id = id))",nativeQuery = true,)
fun findPostsByTagId(id: String): Optional<List<Post>>
How to make this work?
Thanks in advance.
This did the trick:
#Query(value = "SELECT * FROM post WHERE ID IN (select post_id from
post_tag tag WHERE (tag_id = :id))",nativeQuery = true,)
fun findPostsByTagId(id: Long): Optional<List<Post>>
add ":" in front of "id" like sidgate says
changing String to Long for the passed parameter

trying to Fetch Single record from table in laravel using ajax

here is my code that i write in route file
`Route::get('/{username}/ajax-lead', function(){
$brand_id = Input::get('brand_id');
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
$table_lastRecords = DB::table($table_name)->get();
$columns = Schema::getColumnListing($table_name);
$table_lastRecords = DB::table($table_name)->get();
return Response::json($table_lastRecords);
});`
here is my view file
`{{Form::open(array('url'=>'','files'=>true))}}
client list *
Select Clients
#foreach($brand as $userLeads){
brand_id }}">{{ $userLeads->name }}
}
#endforeach
{{Form::close()}}
$('#brand_name').on('change',function(e){
// body...
console.log(e);
var brand_id = e.target.value;
//ajax Call
$.get('/{username}/ajax-lead?brand_id=' + brand_id,function(data){
//success data
console.log(data);
});
});
`
i want to get table_name column value in response. i use json for response.
ANSWER: Your easiest solution would be to return an array of data, where the first value is the table_name and the second value is the collection of lastRecords. It would look something like this:
$responseArray[0] = $table_name;
$responseArray[1] = $table_lastRecords;
return json_encode($responseArray);
Then, in your view, you would parse your Json response and let table_name = responseData[0] and lastRecords = responseData[1]. Of course, you could also use non-numeric indices to make the code more readable, such as responseData['table_name'], but those semantics are up to you.
ADDITIONAL INFO:
With that said, there are several issues with your code that I feel I should point out. You should become more familiar with Eloquent queries, as using them properly will greatly simplify your code. For instance, these two lines:
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
Could be re-written as:
$table_name = User::where('brand_id',$brand_id)->first()->table_name;
But this makes me ask the question, is there only one table per brand? If so, why do you store this information with the user, and not in a 'brands' table? The way that this would make the most sense would be to have a Brands model that has a table_name column, and you would get the table name with this simple query:
$table_name = Brand::find($brand_id)->table_name;
In addition, you should avoid doing this type of work in your routes file. Anything not related specifically to ROUTING the user should be handled in the controller or model. Then, within your controller, you should be utilizing the User or Brand model in your queries by importing App\User and App\Brand.

Undefined property $id for users. But user has ID

I'm attempting to build up a query in Laravel.
I have two tables, with the following attirbuteds
User
id (auto)
name
email
userType
password
TenantPreferance
id (auto)
county
type
user_id (this is the user who created their
preference)
I'm trying to get a collection of data from users of a certain type where id of the user, matches the user_id in preferences.
But I get this error
Undefined property: Illuminate\Database\Eloquent\Builder::$id
$tenants = User::where('userType', 'tenant');
$Prefereances = TenantPreferance::where($tenants->id, $Prefereances->user_id);
$users = $Prefereances->get();
for : $tenants = User::where('userType', 'tenant');
must add:
first()
$tenants = User::where('userType', 'tenant')->first();
or : get()
$tenants = User::where('userType', 'tenant')->get();
You need to actually run the built sql. You also need to get a singular event of it out. So im using first() in this instance. Like this
$tenants = User::where('userType', 'tenant')->first();
$tentants = User::with('TenantPreferance')->where('userType', 'tenant')->get();
where TenantPreferance is the relationship name

SQL query using code igniter

How can I write this query to a code igniter query?
WHERE
SmallVersion.ID =
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)
this is how is looks so far:
->where('SmallVersion.ID', ("SELECT `ChildSmallVersionID` AS ID
FROM `SmallVersion` WHERE ID = $id"));
Mateusz Wojtula's answer looks ok but you can protect your query
$strSubQuery = $this->db
->select("ChildSmallVersionID AS ID")
->from("SmallVersion")
->where("ID",$id)
->get_compiled_select();
$where = "SmallVersion.ID = (".$strSubQuery.")";
$this->db->where($where, NULL, FALSE);
There is no possibility to create brackets in Active Records but you can write it directly in your clause:
$where = "SmallVersion.ID =
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)";
$this->db->where($where, NULL, FALSE);
If third parameter is set to FALSE, CodeIgniter will not protect your query.
Documentation for active records:
https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
if you want to apply this query
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)
can make pice of query
$this->db->group_start()->select('ChildSmallVersionID AS ID')->where('ID', $id)->group_end();
and let make this query to string
$_sub_query = $this->db->group_start()->select('ChildSmallVersionID AS ID FROM SmallVersion')->where('ID', $id)->group_end()->get_compiled_select('SmallVersion');
and apply like
->where('SmallVersion.ID', $_sub_query);

Laravel avoid relationship query on laravel after object creation

How to avoid querying relationship after object creation?
$store = new Store();
$store->name = 'Store 1';
$store->save();
return response()->json($store->products());
Laravel is querying products table. I would like to avoid that since I know there is none. This is just an example.
return response()->json([]); is not an option. In real world I really need to use $store->products().
Use the wasRecentlyCreated property of model to identify if the model was inserted during the current request lifecycle.
$data = $store->wasRecentlyCreated ? [] : $store->products();

Resources