Multiple Projectors on same ReadModel in Broadway CQRS - php

I have a Customer Readmodel that contains some properties including an active property.
Now suppose I would want to fill 2 tables with customer information: for example, one table with just the customer id and active status, and another table containing all info.
Should I make a seperate Projector, Readmodel and Repository to achieve this?
EDIT:
Example scenario:
CustomerCreatedEvent -> contains all customer data
CustomerActivatedEvent -> only contains customer id with enabled status = true
Readmodel = Customer class
A Projector handles both events.
The CustomerActivatedEvent handler will load the customer and update the 'active_users' table through the CustomerRepository (or ActiveCustomersRepository?) based on the data in CustomerActivatedEvent.

No.
The event handlers of your read-side are the ones responsible for updating what you call Readmodels the way you want.
For instance you can have a CustomerCreatedEventHandler which updates the 2 tables you need, or you can have two separate event handlers, both subscribers of the same event, each one updating a different table.

Related

Good Practices MVC

Assume I had a database with two tables:
customer
payment_info
The table customer had fields: name, address, payment_info_id
payment_info_id is a foreign key that links to a row in payment_info. payment_info has fields: id (which is linked to payment_info_id), bank, account number.
Because of my application's logic, each time a customer is created their payment information is stored in a separate row in the payment_info field which is linked to their row in the customer table.
I am using an MVC architecture.
Does it make more sense to keep this 'pure' and make two separate models, one for each table, and then crossload them and call methods from payment_info_controller in customer_controller, or to make a hybrid one which as soon as it creates a row in customer also creates one in payment_info?
Hope this makes sense!
You shouldn't be relying on controllers to do this. Put the logic in your model, behind a service layer(if desired), and call that from your controllers.
You should also reverse the FK setup. Stick customer id in payment_info and remove it payment_info_id from customer. That will make it so a customer can have multiple payments.
in the controller:
$user = new User(name, etc...);
$payment = new Payment(payment details...);
$user->applyPayment($paymentInfo);
$userService->save($user)
in the service:
function save($user){
//save user and update it with an id)
$user = $userDal->save($user);
//now that user has an id, you can generate/save payments.
$payment->save($user->getPayments());
}
You could then have the userService load the Data access layer for both user and payment.
Service/Facade info: http://www.dofactory.com/net/facade-design-pattern
NOTE: - you might want to come up with a better naming convention so its obvious that the payment will be saved/generated by that particular service. e.g SaveUserAndPayment($user)
Always keep it pure you dont need more unused methods to load than you could also from customer you do stuff while you have some customer params and need get something related to it, from payment you do stuff when you have some payment params and need dig something from that. You can always use payment or customer or any other models from any controller so keep it as pure as you can gl and hf ;)

unsure about database logic for multiple items

I am trying to get my head around an issue relating to database logic.
I have a system that is to allow the user to create an event, performances and multiple different ticket types for a given event. These will then be added to the database with prices relating to the ticket types for a given event (the ticket types can be reused for other events and there is no set number of types for each event) and then a customer will go onto the site, select one of the events, performances and will then have listed for them to choose from the different ticket types with prices.
At this point I have a table for events which is using a series to store the ticket ids which are stored in a separate table and yet another table which stores the prices. The use of the series is ridiculous as it tends to crap out on me and either fails to work (as mysql doesn't handle the code properly) or it is incredibly limiting on what can be done with the info Has anyone any better idea how I might achieve this result?
example of an event:
event name: 'event 1'
performance: '23/03/13 (12:30)'
ticket types: Adult (€20), Student (€15), Special (€10), etc
the person setting up the event can create any ticket types they want or use existing ones in the system and just have a price set for this particular event.
If I understand you correctly, I believe what you are doing is most likely the best way to do it.
A user can create multiple events, each of which can have a variety of tickets. Tickets are not specific to an event (can be used on multiple events), and thus the price can not be stored with the ticket information.
Therefore, what you want to do is have these tables:
events - Stores information on the event
tickets - Stores information on the ticket
*events_tickets* - a join table for events and tickets (As it is a many to many relationship)
The events_tickets table would have columns like so:
primary id, event_id (Foreign Key), ticket_id (Foreign Key), price
Hope that helps.
table Event: Id_Event, Ds_Event, Dt_Event, Id_Venue
table Ticket_Type: Id_Ticket_Type, Ds_Ticket_Type, Ic_Ticket_Type_Is_Custom (boolean)
table Event_Ticket_Type_Price: Id_Event, Id_Ticket_Type, Nr_Ticket_Price
table Venue: Id_Venue, Ds_Venue, Ds_Venue_Address

Correct way to change user table relationship?

I am working on an event system that has two tables, EVENTS and EVENT_CREATORS. I have been linking events to creators by placing creator id in the events table as I thought I would only ever have one event creator.
I now need the ability for more than one creator to edit an event. Should I add simply add additional fields to EVENTS table, i.e ec_id, ec_id_2, ec_id_3, etc. Or does it make more sense to add a cross reference table to the database and a separate table for additional creators?
This is those cases, where it would be wise to use a cross reference table. I will explain it step by step. First
Create a new table. Call it "event_reference"
Give the following FIelds: Id, Ref_Id, Creator_ID.
I will omit the need of the EventId, because we are creating a table which is a reference to the event, so event's table will hold the Ref_Id to keep in track of all the references.
Next, Modify the events table and store Ref_ID instead of Creator
In such way, you can fetch all the creators of an events in the normalized way.
You should have 3 tables:
Event (with an ID field)
Creator (with an ID field)
EventCreator (2 fields: eventID and creatorID)
This should pretty much cover every possible relationship between events and creators. You can limit the relationships by creating indexes on the EventCreator table.
The simple say is to just add a cross reference table. This way you don't have to worry about how many creators someone will need in the future.
So, have a table like:
xref_Events_Creators
EventId
CreatorId

CakePHP - conditions on associated model

Problem in short: I want to set
conditions on an associated model. How
do I do it?
I'm having a problem with the associations in my CakePHP application.
The associations looks like this:
Event has many EventSum belongs to Account has many AccountUser belongs to User
Event has many EventDebt ... (the rest is the same as above)
Event also belongs to User
The application is a private econonmy program in PHP and uses the CakePHP framework.
An Event is a financial event, a purchase, transaction between accounts etc. It only holds information about date, title and user.
An EventSum holds information about an Account and how much to debit or credit (in one column, just positive or negative).
Account holds information about title of the account.
AccountUser holds an id of an Account and a User. This indicates that
So, now I want to fetch Events based on what accounts a User is associated to. How can I do this?
I want to fetch the following info:
Event, together with the EventSum. The Events are fetched from Accounts where the User has access.
Thanks for any help,
/Magnus
It seems like you want to be able to query your Event class with the following conditions:
'Account.user_id =' => $userId
Is my assumption correct?
When doing queries which require conditions on associated models, you can use either the Containable behaviour (comes with CakePHP 1.3) or the 'Linkable' behaviour (which can be found here).
What happens when you try this (be sure to attach the Containable behaviour to your models first):
$condition = array('Account.user_id =' => $userId);
$contain = array('EventSum' => array('Account'), 'EventDebt' => array('Account'));
$result = $this->Event->find('all', compact('condition', 'contain'));
Note that you might experience issues when 'containing' both EventSum and EventDebt if both of their associations to Account use the same alias name.

Zend Framework: Handling of multiple database records in one form as checkboxes

I'm new to zend framework but have made my first steps with it successfully.
Until now I have created some Zend_Forms which are mapping single records of my model
to the form fields. I have handled the forms with form classes for each case.
This works all very well until now.
Now I have the situation that I have to asign features to a product. Features and products are parts of my application. Features are stored in my database in three tables. For each feature there is one record in the third table.
First is the feature group where the name of the feature group is saved. Every feature should be asigned to a feature group.
Second table is the features table. This table has an foreign key to the feature group and the name of the feature.
Third table is some kind of many-to-many relation which connects features to products. This table has an aditional field which contains an optional value (beside the two foreign keys) for this unique feature of the product.
For example: if the product has a weight of 4,78 kg the value "4,78" is stored in the third table and the label "weight of %s kg" is stored in the second table. The feature group could be something like "physical attributes" had is saved in the first table.
To cut a long story short:
My problem is how to handle the case that I have to create and edit multiple database records in one form. The plan is to have a form with many checkboxes for each for a feature whereby features are thematicaly grouped. Every checkbox should have an aditional text field to input optional values.
you could make a custom form class that extends Zend_Form and use that for you classes.
It could take in the construct instances of your models and construct the form inputs based on that models.
After form validation in your controller you can do
$values = $form->getValues();
and use that array to populate your models again
You can try creating subforms (Zend_Form_SubForm) inside your form class. This can separate fields for different tables. For edition, in your controller, when you pull all the data from the tree tables, you can populate subforms that correspond to the tables.
You can try to extend Zend_Form to create your own elements.
You will be able to write a class that connects to DB to get attributes (features & products).
Assuming you wrote My_Form_Element_Features & My_Form_Element_Products classes, you can do $features = new My_Form_Features(); and then use the base class methods like getValues(), populate(), etc.
You can take a look there to start :
http://framework.zend.com/manual/en/zend.form.elements.html
http://smartycode.com/extending/database-aware-select-elements/
--
To answer to your comment, you can use :
Zend_Form::setElementsBelongTo($array):
More information can be found at Zend_Form Advanced manual page.

Categories