How to delete all user roles in Kohana 3 - kohana

I'm using ORM Auth module and it's difficult to figure out how to do it. I've tried this case:
$user = ORM::factory('user', $id);
$user->roles->delete_all();
And got error ErrorException [ Fatal Error ]: Call to undefined method Database_Query_Builder_Delete::join()
However $user->roles->find_all(); gives me exactly what i want.

According to version 3.1.3.1 code for Kohana_ORM class, the ORM method "remove($alias, $far_keys=NULL)" if you do not pass the second parameter, it will destroy all related entries.
$user->remove('roles');

Instead of deleting roles from the database, what you want to do is remove the relationships between the user model and the roles model. You can use the ORM remove() method.
foreach ($user->roles->find_all() as $role)
{
$user->remove('roles', $role);
}

Just create a ticket for this feature. You can use a code suggested.

Related

How can I select from a raw string in objection orm

I am trying to run a query from the results of another using with in the objection orm
ex:
Model.query().with(alias, query).select(columns).from(alias);
according to the Knex documentation which is linked from the objection docs, this should work fine. However, when I run the code, objection prepends the schema name to the alias and I get an error stating that relation schema.alias does not exist. I tried using raw but this did not help either.
ex:
Model.query().with(alias, query).select(columns).from(raw(alias));
is there a way for me to select the table/alias defined in the with method without objection prepending the schema to it?
The query method of the model I was using was overridden with code that specified the schema
ex:
class MyModel extends BaseModel {
static query() {
return super.query().withSchema(schema);
}
}
To get around this issue I used the query method of the parent class directly rather than the overridden query method of the model I was using.
This solves my current problem, but does not answer the question of whether one could omit the prepended schema name in the from method.

Why TypeOrm advises to create empty object via `new EntityClass()` THEN only pass fields value?

I'm using NestJS (not Next) NodeJS framework
When I'm creating new objects I used to use new OjbectClass({...fieldsValues});
It's great especially when you use transform pipes from class-transformer;
Besides this approach is used for entity creating:
https://docs.nestjs.com/techniques/database#separating-entity-definition
But as far I see in different guides of TypeOrm usage
here: https://typeorm.io/#/ ,
and here: https://orkhan.gitbook.io/typeorm/docs/entities .
They show first to create an empty object, then only set fields with values:
const object = new EntityObject();
object.field = 'value';
Why? Does it make sense?
Does NodeJS create a redundant hidden class of properties passed via object into Entity Class constructor? If yes - then we can pass coma-separated arguments
I believe it's just cause that's how the docs are. Looking at the code for BaseEntity it does not look like having a constructor to assign the fields would be a problem

Entity Framework Code First check Database exist

In Entity Framework Code first i want to check database is exist before create Database.
In code first when i call Entities dc = new Entities() then it goes to OnModelCreating and generate Database. How can i check if the Database exists in Entity framework Code first?
You can do:
using(var dbContext = new MyContext())
{
if (!dbContext.Database.Exists())
dbContext.Database.Create();
}
Edit:
Following the colegue sugestion, the meaning of this code is very simple: Supose your context constructor is not set to create the database, so before sending any database operations, you can check if it exists, if not, you can create a new one with the connection string parameters being the rules for the creation.
This would be a static alternative, that works even without creating the DbContext first:
System.Data.Entity.Database.Exists(dbNameOrconnectionString);

Kohana 3.3 ORM - Database Views

Is there a way to use the orm factory to pull from a view instead of a table? I was hoping that the syntax would be equivalent to pulling from a table:
$buyers = ORM::factory('vbuyer'); //where vbuyers is the name of the view
This results in the error : ErrorException [ Fatal Error ]: Class 'Model_Vbuyer' not found
Unfortunately that doesn't seem to work. Can I have the orm map a view to a model, or do I have to use the DB::select('*')->from('vbuyers') approach?
EDIT: The problem was actually that the filename of the vbuyer model was incorrect, it had an underscore v_buyer. Removed the underscore and it worked.
EDIT: To answer the original question - yes, you can use the ORM factory to generate models based on database views. The problem in this case was unrelated, see above.

Kohana helper attribute

I have a question that keeps bothering me. Currently, I have started using Kohana 3.2 Framework. I've written a helper to handle some functionality - I have a number of methods, which are (as it should be) declared STATIC. But, all of these methods are somehow working with the database, so I need to load a model. Currently, every method has a non-static variable like this:
$comment = new Model_Comments;
$comment->addComment("abc");
OK, it seems to be working, but then I wanted to get rid of this redundancy by using class attribute to hold the instance of the model (with is class as well).
Something like this:
private static $comment; // Declaring attribute
self::$comment = new Model_Comment; // This is done within helper __constuct method
self::$comment->addComment("abc"); // And call it within the method.
But, I got failed with: Call to a member function addComment() on a non-object
Question is: is it possible to do it ? Maybe there are some other approaches ?
Sorry for a long story and, thanks in advice! :P
A static method cannot call a non-static method without operating on an instance of the class. So, what you're proposing won't work. There may be a way do accomplish something similar, but what about trying the following:
You could implement the singleton or factory pattern for your "helper" class. Then, you could create the model (as an attribute) as you instantiate/return the instance. With an actual instance of your "helper" class, you won't have to worry about the static scope issues.
In other words, you can create a helper-like class as a "normal" class in your application that, upon creation, always has the necessary model available.
I'd be happy to help further if this approach makes sense.
David

Resources