Cakephp condition not working for enum - cakephp

I am trying to apply a condition on my company table in cakephp 2.5
I need to select only the companies with status 1, below is the code:
$this->loadModel('Company');
$fields=array('id','name','logo','status');
$conditions=array('status'=>'1' );
$search_companies = $this->Company->find('all',
array('fields'=>$fields,'conditions'=>$conditions));
this always returns companies with a status of 0 and not 1 as expected. Why is that?
The database type used for status in my table is enum.

Cake does not support ENUM out the box, change the data type to VARCHAR and it will work.

You can try to change your table schema field to varchar or if it's mysql change to an enum type.
Another solution would be:
$conditions=array('status=1' );
if your schema is of type int.

Related

How to convert a CharField to a DateTimeField in peewee on the fly?

I have model I created on the fly for peewee. Something like this:
class TestTable(PeeweeBaseModel):
whencreated_dt = DateTimeField(null=True)
whenchanged = CharField(max_length=50, null=True)
I load data from a text file to a table using peewee, the column "whenchanged" contains all dates in a format of '%Y-%m-%d %H:%M:%S' as varchar column. Now I want to convert the text field "whenchanged" into a datetime format in "whencreated_dt".
I tried several things... I ended up with this:
# Initialize table to TestTable
to_execute = "table.update({table.%s : datetime.strptime(table.%s, '%%Y-%%m-%%d %%H:%%M:%%S')}).execute()" % ('whencreated_dt', 'whencreated')
which fails with a "TypeError: strptime() argument 1 must be str, not CharField": I'm trying to convert "whencreated" to datetime and then assign it to "whencreated_dt".
I tried a variation... following e.g. works without a hitch:
# Initialize table to TestTable
to_execute = "table.update({table.%s : datetime.now()}).execute()" % (self.name)
exec(to_execute)
But this is of course just the current datetime, and not another field.
Anyone knows a solution to this?
Edit... I did find a workaround eventually... but I'm still looking for a better solution... The workaround:
all_objects = table.select()
for o in all_objects:
datetime_str = getattr( o, 'whencreated' )
setattr(o, 'whencreated_dt', datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S'))
o.save()
Loop over all rows in the table, get the "whencreated". Convert "whencreated" to a datetime, put it in "whencreated_dt", and save each row.
Regards,
Sven
Your example:
to_execute = "table.update({table.%s : datetime.strptime(table.%s, '%%Y-%%m-%%d %%H:%%M:%%S')}).execute()" % ('whencreated_dt', 'whencreated')
Will not work. Why? Because datetime.strptime is a Python function and operates in Python. An UPDATE query works in database-land. How the hell is the database going to magically pass row values into "datetime.strptime"? How would the db even know how to call such a function?
Instead you need to use a SQL function -- a function that is executed by the database. For example, Postgres:
TestTable.update(whencreated_dt=whenchanged.cast('timestamp')).execute()
This is the equivalent SQL:
UPDATE test_table SET whencreated_dt = CAST(whenchanged AS timestamp);
That should populate the column for you using the correct data type. For other databases, consult their manuals. Note that SQLite does not have a dedicated date/time data type, and the datetime functionality uses strings in the Y-m-d H:M:S format.

Laravel 5, orderByRaw using field and column doesn't exists

I have problems with Eloquent/QueryBuilder when I use orderByRaw function I got exception column doesnt exists, but the column is already exists into database.
Here is the exception:
SQLSTATE[42883]: Undefined function: 7 ERROR: function field(integer, integer) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The status column into database is integer.
Here is my code:
$orderedStatuses = implode(',', [4]);
$users->orderByRaw(\DB::raw("FIELD(status, $orderedStatuses)"));
...also, if I use users.status I got the same error.
Can someone help me?
Thanks
You mixed both the methods.
$users->orderBy(\DB::raw("FIELD(status, $orderedStatuses)"));
Or
$users->orderByRaw("FIELD(status, $orderedStatuses)");
orderByRaw expects raw SQL, you have to pass string in the argument of orderByRaw.
Example:
$users->orderByRaw('field1 ASC, field2 DESC, field3 DESC,...');

SymmetricDS: No enum constant org.jumpmind.symmetric.io.data.transform.ColumnPolicy.specified

I'm doing multi-homing SymmetricDS with 2 nodes (server and client) and single MySQL database, and I do transformation table and column, but at client node appear an error "java.lang.IllegalArgumentException: No enum constant org.jumpmind.symmetric.io.data.transform.ColumnPolicy.specified".
I (un)delete column_policy from sym_transform_table and nothing happened. What's wrong?
this is going to fix the problem:
update sym_transform_table
set column_policy = 'SPECIFIED'
where column_policy = 'specified';
commit;
I got the answer, I should make column_policy at SYM_TRANSFORM_TABLE in UPPERCASE MODE. And also apply in other enum.

Unable to load a null value into SQL Server using a TableAdapter

I have read all the related entries (here and at other sites) and have not found my situation.
I have a table (MyTable) with several date fields (MyDateField1, MyDateField2, etc) plus other fields not pertinent to this matter. All the date fields allow null values.
My application's tableadapter's insert method invokes the following stored procedure:
INSERT INTO MyTable VALUES (#MyDateField1Value, #MyDateField2Value,..., <other fields>);
using this VB code (dv is a dataview):
NewRow = dv.AddNew
NewRow(MyDate1Field) = DateValue1 <some date value taken off a window>
NewRow(MyDate2Field) = DateValue2 <some date value taken off a window>
....
NewRow.EndEdit()
MyTableTableAdapter.Insert(DateValue1, DateValue2, <etc>)
This works fine when none of the date fields is null. However, if I set:
NewRow(MyDate1Field) = DBNull.Value
I get the error message that DBNull.Value cannot be converted to a date value. How can I get a null value into MyDateField1, MyDateField2, etc.
The answer is to pass Nothing. Very simple - too bad it was so hard to find.

NVarchar Prefix causes wrong index to be selected

I have an entity framework query that has this at the heart of it:
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = N'SOME VALUE HERE'
The execution plan for this chooses an index that is a composite of three columns. This takes 10 to 12 seconds.
However, there is an index that is just [SomeOtherOrderId] with a few other columns in the "include". That is the index that should be used. And when I run the following queries it is used:
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = CAST(N'SOME VALUE HERE' AS VARCHAR(200))
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets
WHERE widgets.[SomeOtherOrderId] = 'SOME VALUE HERE'
This returns instantly. And it uses the index that is just SomeOtherOrderId
So, my problem is that I can't really change how Entity Framework makes the query.
Is there something I can do from an indexing point of view that could cause the correct index to be selected?
As far as I know, since version 4.0, EF doesn't generate unicode parameters for non-unicode columns. But you can always force non-unicode parameters by DbFunctions.AsNonUnicode (prior to EF6, DbFunctions is EntityFunctions):
from o in db.WidgetOrder
where o.SomeOtherOrderId == DbFunctions.AsNonUnicode(param)
select o
Try something like ....
SELECT 1 AS dummy
FROM [dbo].[WidgetOrder] AS widgets WITH (INDEX(Target_Index_Name))
WHERE widgets.[SomeOtherOrderId] = N'SOME VALUE HERE'
This query hint sql server explicitly what index to use to get resutls.

Resources