SQL to QueryBuilder - doctrine-orm

first, sorry for bad English!
I try to convert this SQL (it's operational):
SELECT DISTINCT U.id
FROM User U
INNER JOIN Detail DE on U.id = DE.id_user
INNER JOIN matiere MA on U.id = MA.id_user
WHERE DE.ville = $var1
AND MA.matiere = $var2
in query builder.
I have try this:
$query = $repository->createQuerybuilder('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user' )
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville = :ville')
->setParameter('ville', $ville)
->andWhere('MA.matiere = :matiere')
->setParameter('matiere', $matiere)
->distinct();
but I have this error:
"[Syntax Error] line 0, col 49: Error: Expected end of string, got '.' "
And when I try this:
$query = $repository->createQueryBuilder()
->select('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user' )
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville = :ville')
->setParameter('ville', $ville)
->andWhere('MA.matiere = :matiere')
->setParameter('matiere', $matiere)
->distinct();
I have this error:
Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder(),
I work with doctrine and symfony3.
Thanks for help.

This is like a syntax error , both the function of development in the query builder as the own instance of the query , try to make a debug and see how the SQL query is being built , syntax error possibly.
$qb = $repository->createQueryBuilder();
$query = $qb->getQuery();
$debug = $query->debug();
has a space between the strings [ ' U.id = DE.id_user ' ) ] , remove these spaces to try to surround the possible error;
or you can try make this way, probably work:
$query = $repository->createQueryBuilder(); <!--separte constructor this line-->
$query->select('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', 'U.id = DE.id_user' ) <!--align this line-->
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville', ':ville') <!--separate this line-->
->setParameter('ville', $ville)
->andWhere('MA.matiere', ':matiere') <!--separate this line-->
->setParameter('matiere', $matiere)
->distinct()
->getQuery(); <!--add this line-->
$resp = $query->getResult(); <!--get the answer this line-->

In this line:
$query = $repository->createQuerybuilder('U.id')
You are trying to pass a object and method call instead of just a single parameter. That why the error about the dot.
Use this instead before the querybuilder to get the ID:
$id = U.id
or
$id = $U->getId();
then pass in parameter:
$query = $repository->createQuerybuilder($id )
...
Then you won't get the error: "[Syntax Error] line 0, col 49: Error: Expected end of string, got '.' "

Related

string concat causing 'str' object has no attribute 'object'

I have a view like below:
I get the error: 'str' object has no attribute 'object'
it must have something to do with my string concat - what has gone wrong :)
thanks
def edit_skill_plan(request):
#t = skills.objects.get(skill_id='django_5158517')
skillid = request.POST.get('editbut')
user = request.user
t = skills.objects.get(creator=user, skill_id=skillid)
t.status = 'closed'
t.save() # this will update only
complete_date = datetime.today().strftime('%Y-%m-%d')
week_num = datetime.today().strftime('%V')
x = t.skill_name
user = request.user
points = t.points
cat = t.category
year = datetime.today().strftime('%Y')
status = str(request.user.username) +' completed the task: ' + str(x) + ' which was worth: ' + str(points) + ' points'
status.object.create(creator=user, status=status, status_date=complete_date)
Your last two line should be,
status_code = str(request.user.username) +' completed the task: ' + str(x) + ' which was worth: ' + str(points) + ' points'
status.object.create(creator=user, status=status_code, status_date=complete_date)

Getting Admin Product Search to Ignore Special Characters in Opencart

I have a client who is wanting the product search in the admin area to ignore the fact that there are special characters in the product names.
For example, they have a lot of products listed with the word “kings” or the word “king’s” with an apostrophe - both used correctly grammatically depending on the product.
What they want to be able to do is search for “kings” on the admin area and all products called “kings” and “king’s” appear - rather than just “kings” as it does by default.
They have the same problem with “remy” and “r.e.m.y” where, when searching for “remy” the search needs to ignore the fact that there are “.” In the name.
Opencart Version 2.3.0.2
Any help is greatly appreciated!
Look, if you have expirience you can do it easily, just find search query in Opencart and replace all special symbols with empty string, check this example if you have r.e.m.y in your database table blog, title column with value 'r.e.m.y, then this query will return you record
SELECT * FROM `blog` WHERE title = replace(title,'.','') = 'remy'
Check image for example:
Example is for points '.' only but you can do replace and for other special symbols.
Let me show you where to find this query:
First you can see link is catalog/product, so your controller is
in admin/catalog/product, when u type something in "title" input, it send GET parameter &filter_name to this controller. You can check the controller for that filter, see:
protected function getList() {
if (isset($this->request->get['filter_name'])) {
$filter_name = $this->request->get['filter_name'];
} else {
$filter_name = null;
}
Here it check for this GET parameter and set it to variable $filter_name, then bellow the code it send this variable to model:
$filter_data = array(
'filter_name' => $filter_name,
'filter_model' => $filter_model,
'filter_price' => $filter_price,
'filter_quantity' => $filter_quantity,
'filter_status' => $filter_status,
'filter_image' => $filter_image,
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $this->config->get('config_limit_admin'),
'limit' => $this->config->get('config_limit_admin')
);
$product_total = $this->model_catalog_product->getTotalProducts($filter_data);
It set array with variables then u call $this->model->catalog->product->getTotalProducts, so there is query u need:
public function getTotalProducts($data = array()) {
$sql = "SELECT COUNT(DISTINCT p.product_id) AS total FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id)";
$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
if (!empty($data['filter_name'])) {
$sql .= " AND pd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
}
if (!empty($data['filter_model'])) {
$sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
}
if (isset($data['filter_price']) && !is_null($data['filter_price'])) {
$sql .= " AND p.price LIKE '" . $this->db->escape($data['filter_price']) . "%'";
}
if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
$sql .= " AND p.quantity = '" . (int)$data['filter_quantity'] . "'";
}
if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
}
if (isset($data['filter_image']) && !is_null($data['filter_image'])) {
if ($data['filter_image'] == 1) {
$sql .= " AND (p.image IS NOT NULL AND p.image <> '' AND p.image <> 'no_image.png')";
} else {
$sql .= " AND (p.image IS NULL OR p.image = '' OR p.image = 'no_image.png')";
}
}
$query = $this->db->query($sql);
return $query->row['total'];
}
If you don't have expirience with Opencart it will be not so easy, but it's possible.
One hint: u can use your own query if this parameter is set (if-else) :)

Using where in salesforce query

I currently have this statment:
$query = 'SELECT DataStream__c from JUS_Contract__c limit 50';
Which works fine, but I want to only select the datastream when Datavalue = 3;
for example:
$query = 'SELECT DataStream__c from JUS_Contract__c where DataValue__c=3 limit 50';
When this is atempted I get a notice saying:
Trying to get property of non-object
I know this is because DataValue returns something like this:
stdClass Object ( [Id] => [DataValue__c] => 3)
When I do:
$query = 'SELECT DataStream__c from JUS_Contract__c limit 50';
$response = $mySforceConnection->query(($query));
//print_r($response->records);
foreach ($response->records as $record) {
print_r($record);
(for printing I fixed it by doing this: echo $record->DataStream__c; )
However I can not do that in the query, and am unsure how to change it to either a string or a int inside the query so that I can use the where command on it. Any suggestions would be great.

Doctrine 2 WHERE IN - invalid input syntax for integer: "Array"

I am trying to get user accounts for specific users.
BASIC CODE:
$ownerIds = array();
foreach ($owners as $owner) {
$ownerIds[] = $owner->getId();
}
$qb = $this->entityManager
->createQueryBuilder();
THEN I tried following:
1.
$qb->select('a')
->from('Account', 'a')
->andWhere('a.owner IN (?1)')
->setParameters(array(
1 => $ownerIds
);
2.
$qb->select('a')
->from('Account', 'a')
->add('where', $qb->expr()->in('a.owner', '?1'))
->setParameters(array(
1 => $ownerIds
);
I also tried to switch question mark with parameter name ':name'
$qb->select('a')
->from('Account', 'a')
->add('where', $qb->expr()->in('a.owner', ':name'))
->setParameters(array(
':name' => $ownerIds
);
But I am still getting this error: Invalid input syntax for integer: "Array"
Try this:
$qb->select('a')
->from('Account', 'a')
->andWhere($qb->expr()->in('a.owner', $ownerIds));
Hope this helps.

Validation for Form and QueryString in ASP Classic using Regex. Almost working but missing something?

I'm trying to add some Input Validation in Classic ASP by using the function/code seen below.
The only one that looks like it's working correctly is the "text" type. the others I keep getting errors or it just does not filter correctly.
I'm trying to understand what I'm doing wrong please help me.
Valid Data Types: "email", "integer", "date", "string" and "text".
The first three are obvious, the last two have slight differences.
The "email" should only allow numbers and leters, and the following characters "#" , "-" , "." , "_"
The "date" should validate by running IsDate and if True then allow if False DON'T.
The "string" should validate text-based querystrings, allowing only letters, numbers, _, - and .
Whereas "text" is any free-form text form field type content.
The "integer" should only allow numbers and a period (.)
Usage Example: <input type="text" value="<%=MakeSafe("test#test.com</HTML>1234.5",integer,50)%>">
Eg: MakeSafe(dataInput,dataType,dataLength)
<%
'// CODE BY: dB Masters
'// FOUND AT: http://successontheweb.blogspot.com/2008/03/input-validation-for-security-in.html
Function MakeSafeConvert(encodeData)
encodeData = replace(encodeData,"&", "&")
encodeData = replace(encodeData,"'", "'")
encodeData = replace(encodeData,"""", """)
encodeData = replace(encodeData,">", ">")
encodeData = replace(encodeData,"<", "<")
encodeData = replace(encodeData,")", ")")
encodeData = replace(encodeData,"(", "(")
encodeData = replace(encodeData,"]", "]")
encodeData = replace(encodeData,"[", "[")
encodeData = replace(encodeData,"}", "}")
encodeData = replace(encodeData,"{", "{")
encodeData = replace(encodeData,"--", "--")
encodeData = replace(encodeData,"=", "=")
MakeSafeConvert = encodeData
End Function
Function MakeSafe(dataInput,dataType,dataLength)
Dim regex, validInput, expressionmatch
regex = ""
validInput = "1"
If dataType = "string" And Len(dataInput) > 0 Then
regex = "^[\w-\.]{1,"& dataLength &"}$"
ElseIf dataType = "email" And Len(dataInput) > 0 Then
regex = "^[\w-\.]+#([\w-]+\.)+[\w-]{2,6}$"
ElseIf dataType = "integer" And Len(dataInput) > 0 Then
regex = "^\d{1,"& dataLength &"}$"
ElseIf dataType = "date" And Len(dataInput) > 0 Then
If Not IsDate(dataInput) Then validInput = "0" End If
ElseIf dataType = "text" And Len(dataInput) > 0 Then
If Len(dataInput) > dataLength Then validInput = "0" End If
End If
If Len(regex) > 0 And Len(dataInput) > 0 Then
Set RegExpObj = New RegExp
RegExpObj.Pattern = regex
RegExpObj.IgnoreCase = True
RegExpObj.Global = True
RegExpChk = RegExpObj.Test(dataInput)
If Not RegExpChk Then
validInput = "0"
End If
Set RegExpObj = nothing
End If
If validInput = "1" And Len(dataInput) > 0 Then
MakeSafe = MakeSafeConvert(dataInput)
ElseIf Len(dataInput) = 0 Then
MakeSafe = ""
Else
Response.Write "<h2>Processing Halted.</h2>"
Response.End
End If
End Function
%>
EXAMPLE CODE AND ERROR(S):
When I test this using the code:
<%=MakeSafe("test#test.com1234.5",email,50)%>
* Does NOT Validate Anything.*
I don't get an error message but it DOES NOT Validate anything.
**The OUTPUT IS : test#test.com1/27/20121234.5
SHOULD BE ONLY: test#test.com**
When I test this using the code:
<%=MakeSafe("test#test.com1/27/20121234.5",date,50)%>
I don't get an error message but it DOES NOT Validate anything.
The OUTPUT IS : test#test.com1/27/20121234.5
SHOULD BE ONLY: 1/27/2012
The other two give me this error message:
<%=MakeSafe("test#test.com1234.5",string,50)%>
* ERROR!!! Wrong number of arguments or invalid property assignment: 'string'
<%=MakeSafe("test#test.com1234.5",integer,50)%>
* ERROR!!! Syntax error
Thank you so much for any help that you provide...
If it's not a typo then your fault was in the second parameter of the function call.
You call the function like:
<%=MakeSafe("test#test.com1234.5",email,50)%>
which is wrong because you should "..." the second parameter too. This should work:
<%=MakeSafe("test#test.com1234.5","email",50)%>