I have a table called account. I would like to create a repository called accountSettings using the account table. The reason I wouldn't want to create a table called accountSettings is because all the fields I need already exists in account table. However, I would like to have a dedicated repository for the settings pointing to the account table. How can I do this?
You are looking for how to manually map a model to an arbitary DB table. So something like this in your Configuration/Extbase/Persistence/Classes.php should help you get started:
<?php
declare(strict_types = 1);
return [
\MyVendor\MyExtension\Domain\Model\Account::class => [
'tableName' => 'account',
],
\MyVendor\MyExtension\Domain\Model\AccountSettings::class => [
'tableName' => 'account',
],
];
This way you can add properties to your model which match your domain. If necessary you can also define custom properties mapping here too, see the linked page.
Related
I've a web app, being used by engineers for Asset(machines, scales) calibrations on a Site & certificate can be generated based on those readings. Up till now, the requirement was so that for a given SITE includes multiple ASSET and each asset has its own CALIBRATION.
So had my Modal like this.
With a recent change to include a new type of certificate. Where an ASSET can have multiple calibration (Two to be exact) one before Adjustment and One after if needed.
My question is, what is the best way to accommodate this change? Should I change the relation between ASSET one-to-one CALIBRATION to one-to-many with multiplicity (1..2) which basically requires to change lot of code check. or should adding another column in ASSET table which points to another entry within the same table. Or is there any other approach to opt ?
I'm using ASP.MVC, with Entity Framework.
Mapping
public Report_AssetMap()
{
HasKey(one => one.report_asset_id);
// Site_Report one-to-many-rel Report_Asset
HasRequired(one => one.Site_Report).WithMany(one => one.Report_Assets).HasForeignKey(one => one.site_report_id);
// Report_Asset one-to-one-rel Asset_Calcert
HasOptional(one => one.Asset_Calcert).WithRequired(ad => ad.Report_Asset).WillCascadeOnDelete(true);
}
public Asset_CalcertMap()
{
HasKey(one => one.report_asset_id);
// User one-to-many Asset_Calcert (with nullable Calcert_handled_by_id at many End)
HasOptional(o => o.Calcert_Handled_By).WithMany(r => r.Handled_Calcert).HasForeignKey(o => o.calcert_handled_by_id);
}
I would have a separate table for each of the pre and post adjustment certs; this is a solid use case for table-per-concrete-class inheritance (which is not yet included in EF Core but I'm guessing you're using EF6). Put common properties in a base class, derive a class for your pre and post adjustment certs (can be empty if no differentiation other than table names), then MapInheritedProperties and specify different table names for the derived classes in the model configuration.
https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines
If you then want to query against all Asset_CalCerts, you can specify a single DbSet<Asset_Caclert> DbSet in the DbContext to query while your Report_Asset entity can have a property referencing each derived Asset_CalCert type.
In this way you can keep your 1:? relationship while maintaining the ability to query all Asset_CalCerts as though they were in a single table.
I am trying to generate scaffolding for STI implementation. I issue the following.
rails g scaffold user1 type name email
rails g scaffold member company subscription --parent user1
Every thing gets generated file except for the migration file my 'member' model.
When I try to create a member record like this
Member.create(name: "My Name", email: "myname#example.com",
company: 'Example LLC', subscription: 'Monthly Gold' )
I get this error:
ActiveModel::UnknownAttributeError: unknown attribute 'company' for Member. from (irb):1
Any ideas on what is going on?
I use rails 5 and db is postgres
The --parent option assumes that you are already all setup for single table inheritance, i.e. the parent class has a table with a type column (or whatever column you are using for this).
Since the model will be stored in the parent's table, there is no need to create a new table for the subclass, hence no migration
I got this answer similar to this question asked by someone.
To my understanding, you are on the wrong track. In single table inheritance, all the attributes must be present in the parent model table with an additional column name 'type' to indicate the type of inherited model. The column name 'type' can be changed with appropriate settings but ActiveRecord by default looks for 'type' column. You are getting 'UnknownAttributeError' error cause the parent model does not have the following column in its table. You need to write a migration to add the new columns. Hope you understand the concept of STI. For further exploration, I am providing you the link of the official guide. Hope your problem will be solved.
http://edgeguides.rubyonrails.org/association_basics.html#single-table-inheritance
In SugarCRM 7.5 when you open Contacts and view a record in the Opportunities subpanel you can link Opportunity record and set contact_role (Opportunity Role) that is a custom relationship field existing only in opportunities_contacts in the database.
Now I need the same functionality working with my two custom modules with many-to-many relationship between them and access_rights as the custom relationship field.
Two custom modules were created using Module Builder and deployed: c_Accounts, c_Users.
The many-to-many relationship between c_Accounts and c_Users was created in Studio.
in custom/metadata/c_accounts_c_usersMetaData I added
5 =>
array (
'name' => 'access_rights',
'type' => 'varchar',
'len' => 36,
),
and then Quick Repair & Rebuild. I accepted the SQL query and access_rights field was added to my relationship table in database.
Next, I added into both custom modules custom/Extension/modules/{module}/Ext/Vardefs/account_user_access_rights.php
and I was able to see the field in Studio subpanels and add the column to the subpanels view.
Now, the problem is the columns are shown properly in the subpanels for both custom modules but there is no data shown from the database. How to get the data from the link table into the subpanels?
After the above is accomplished. The next thing is to be able to edit/save the field in the subpanels.
The tools (such as Module Builder) do not support this out of the box. For example, the Contact Role field you mention is implemented as part of ContactOpportunityRelationship bean within Contacts module.
If you look at how the opportunity role is defined in Contacts metadata, you'll see you need to make sure to add the right "rname_link" to the field definition. This will make sure that SugarQuery (which is used to populate Subpanels in 7.5) adds the right Join to populate your subpanel.
In short, does Laravel's eloquent have something like it's sync() functionality that doesn't save the relationships to the database right away?
I have two models
Place (id, name, address);
and
Feature (id, name);
These two models has a has_many_and_belongs_to relationship with each other, using the pivot table
feature_place(place_id, feature_id). This works as expected. Now I'm trying trying to build a form for creating a new Place. The form contains a checkbox for each existant Feature. For error handling I want to be able to create a Place object when the form is submitted. I'd like to be able to create my HABTM relationship, so I can easily rebuild and fill the form if submission fails (validation error or similar). sync() would usually handle the relationships, but since the submission has failed I don't want to insert the Place in the database yet.
Is there an easy way, like sync(), to create the relationship between my Place and it's Features that I can use to repopulate my form on rebuild, without having to write to the database before form submission succeeds?
It sounds like you need to flash the input to the session and then load the old values when the form is rendered.
// Somewhere in your post route
if ( $oh_noes_teh_validator_failed )
{
Input::flash();
Redirect::to_route( 'my-edit-form' );
}
// Somewhere in your edit route or view
$title = Input::old( 'title', $title );
I have a problem when I'm trying to make a form with collections. I explain you what my current scenario is.
I've created two basic objects: Product and Category. And I've created two types for them as well: ProductType, CategoryType.
I have 3 categories and 1 product and I've associated the first couple of categories to the product. So, the product has two categories associated.
I want to create the Product's Form. In this form I want to show only the categories the product has, in a html select control, so the user can make future operations with these data.
I summarize you the key points.
My Product class has
class Product
{
...
#EmbedMany(targetDocument="Acme\StoreBundle\Document\Category")
protected $categories;
...
}
In my ProductType I have:
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('id')
->add('name')
->add('price')
->add('categories', 'document', array(
'class' => 'Acme\StoreBundle\Document\Category',
'choices' => $builder->getData()->getCategories()->toArray(),
'multiple' => 'true',
'property' => 'name'
))
;
}
I've tried with everything and the most accurate half solution was to create the categories property of the ProductType as a document and choose the options through the bind data come from the controller.
The point is, with this solution the ids, of the options controls, are the spl_object_hash set in the UnitOfWork class, not the ids of the original Category object.
My previous tryings:
I don't have problem when I use documents without previous choices, but I want only the categories that Product owns.
I don't have problem when I use a collection to show the list of categories (associating the CategoryType), but I don't know how to show this as a select control.
I can't use a query_builder over Category class because I can't query only the objects have the product id X, because Category object doesn't have any Product reference (and that's right).
Does anyone has a solution for this issue or other idea to solve this?
Thank you very much,
Ricky.
you say
I want to create the Product's Form. In this form I want to show only the categories the product has, in a html select control, so the user can make future operations with these data.
but then you go ahead and grab all the categories.
Why not keep things simple and grab the referenced categories off the product object?
$cats = $product->getCategories();
if(!is_null($cats) && $cats->count() > 0) {
$choices = $cats;
} else {
// grab all so you can have the use set them
$choices = $builder->getData()->getCategories()->toArray()
}
That's not the problem. The problem is when you work with Embedded Documents in MongoDB.
When you're working with these kind of documents, Doctrine asume (with right logic) that you're not going to have a "manual" reference of this embedded document in other collection.
Yes, that seems logic, but... why can't you have a simplified embed document who references other extended version in other collection?. In my opinion, this is a mistake.
As I explained in my question, the ODM makes the id with a hash of the object not with ID anotation. So, you can grab all the options, as you point wisely, but your ids will be different to your Embed's ids.
Thank you again,
Ricky.