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.
Related
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.
I'm having a set of jOOQ classes generated with the jooq-codegen-maven plugin, that's cool! But when I want to use DSLContext to populate schema models by those classes by:
dslContext.ddl(Public.PUBLIC, new DDLExportConfiguration()
.createSchemaIfNotExists(true)
.createTableIfNotExists(true))
.executeBatch();
It got an error:
Caused by: org.postgresql.util.PSQLException: ERROR: type "my_type" does not exist
Debugging generated queries, I only see queries that creating schema and tables
JOOQ version: 3.12.4
As per jOOQ 3.13, we don't support types (enums and others) in the jOOQ runtime meta model yet, so there's no way to re-create them from generated code or from other means. The relevant feature request is https://github.com/jOOQ/jOOQ/issues/9509
The workaround is for you to implement your own mechanism to create these types at the appropriate moment.
When generating code using JOOQ for a SQL Server database the generation creates three-part qualifiers like: [catalog].[schema].[table]. This is exactly what I want when working with the SQL Server databases but is an issue when using the generated code with another database like an H2 in memory database for unit testing.
The H2 dialect does not support these three-part qualifiers, H2 expects something like [catalog].[table]. This causes syntax errors when executing commands like the following against H2:
context.createTable(TBLBUSINESSENTITY).columns(TBLBUSINESSENTITY.fields()).execute();
To solve this I need to change the qualifier at runtime which I thought could be done using a render mapping and mapped schema. Unfortunately, this seems to only have the ability to modify the schema portion of the qualifier like this:
Settings settings = new Settings().withRenderMapping(new RenderMapping().withSchemata(
new MappedSchema().withInput("dbo").withOutput("mySchema")));
Given the qualifier [MyDatabase].[dbo].[MyTable], this maps to [MyDatabase].[mySchema].[MyTable] but I cant figure out how to remove that section entirely.
Is there some way to rewrite the mapping to [MyDatabase].[MyTable]?
Use this setting instead:
Settings settings = new Settings()
.withRenderCatalog(false)
.withRenderMapping(new RenderMapping()
.withCatalogs(new MappedCatalog()
.withInput("MyDatabase")
.withSchemata(new MappedSchema()
.withInput("dbo")
.withOutput("MyDatabase"))));
I've following requirement:
During the model configuration, I create a QueryFilter for an Entity
var entity = modelBuilder.Entity<TBaseTable>().HasQueryFilter(r => r.UserId == CurrentUserId)
So during runtime in some cases the CurrentUserId changes. But my QueryFilter does not get refreshed. The filter criteria still are working with the old UserId value, which I had during the configuration. How do i modify the my QueryFiler, which was set during the configuration? If I'm right then the whole model is already cached and will not be reinitialized. Any ideas about that?
The trick is to declare CurrentUserId as a property on your DbContext class - the property getter can still return a value from a global class instance.
Global filters can be made dynamic by using properties/methods/fields of the db context. See the TenantId example in Global Query Filters documentation.
(Since I needed to read the comments to find the answer to your question I rephrased the comment as provided by "Ivan Stoev Oct 8 '19 at 15:34" and posted it as an answer)
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.