CakePHP, number of views - database

Suppose, this is a db table in my project :
<table>
<tr>
<th>id</th>
<th>title</th>
<th>description</th>
<th>created</th>
<th>modified</th>
<th>........</th>
</tr>
<tr>
<td>1</td>
<td>Some Title</td>
<td>Some Description</td>
<td>7/8/2013 8:50</td>
<td>7/8/2013 8:50</td>
<td>........</td>
</tr>
</table>
Now, I want to get the number how many times record 1 (id=1) has been seen. And that is in CakePHP. Is there any easy way to get this information ? I know created and modified are 2 fields whose values are automatically generated by CakePHP. So, is there any field or something that can be used here ? I marked this as ......... . Or, is there any other way to do this ? Thanks.

There's nothing built-in, but it's not hard to make yourself. (The hard part is to not increment the counter when search engine bots access the page if it's reachable to them.)
In the database create a field viewcount INT DEFAULT 0 for example. In the controller, assuming the model is called Model and action is view (replace with actual names), add the code that increments the counter when the page is viewed:
function view( $id ) {
// ....
$this->Model->updateAll(
array( 'Model.viewcount', 'Model.viewcount + 1' ),
array( 'Model.id', $id )
);
}

CakePHP doesn't have an "automatic" viewed field. If you'd like this functionality you have to implement it yourself with the updateAll method:
$this->Model->updateAll(
array('Model.views', 'Model.views + 1'),
array('Model.id', $id)
);

Related

visualforce emailtemplate issue referencing a list in an Apex class

I suffer an error trying to implement a visualforce emailtemplate. The dev console gives me the following problem on the VF component: Unknown property 'String.Name' (line 0). I can't figure out how to resolve this. Consequently the email template doesnt work & gives me the error: <c:ProductOrderItemList can only contain components with an access level of global.
I aim to send an email from a Product Order (parent). In the email I want to include child records (product order items).
Apex class:
public class ProductOrderTemplate
{
public Id ProductorderID{get;set;}
public List<Product_Order_Item__c> getProductorderItems()
{
List<Product_Order_Item__c> toi;
toi = [SELECT Product_Type__r.Name, Quantity__c FROM Product_Order_Item__c WHERE Product_Order__c =: ProductorderID];
return toi;
}
}
The visualforce component:
<apex:component controller="ProductOrderTemplate" access="global">
<apex:attribute name="ProductOrdId" type="Id" description="Id of the Product order" assignTo="{!ProductorderID}"/>
<table border = "2" cellspacing = "5">
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<apex:repeat value="{!ProductorderID}" var="toi">
<tr>
<td>{!toi.Name}</td>
<td>{!toi.Quantity}</td>
</tr>
</apex:repeat>
</table>
</apex:component>
And the email:
<messaging:emailTemplate subject=“Product Order” recipientType="User" relatedToType=“Productorder”>
<messaging:htmlEmailBody >
Hi,<br/>
Below is the list of ... for your Product order {!relatedTo.Name}.<br/><br/>
<c:ProductOrderItemList ProductOrderId="{!relatedTo.Id}" /><br/><br/>
<b>Regards,</b><br/>
{!recipient.FirstName}
</messaging:htmlEmailBody>
</messaging:emailTemplate>
Any help much appreciated! Quite stuck on this. I'm new to visualforce and apex.
​​​​​​​
You might need just a VF email template without component and apex class. Go to Product_Order_Item__c object, find Product_Order__c field definition. Write down the "Child Relationship Name".
Let's say it'll be "Line_Items". You need to add "__r" to the end (why?) and then you can do something like that:
<messaging:emailTemplate subject="Product Order" recipientType="User" relatedToType="Productorder">
<messaging:htmlEmailBody >
<p>here are your line items</p>
<table>
<apex:repeat value="{!relatedTo.Line_Items__r}" var="item">
<tr>
<td>{!item.Name}</td>
<td>{!item.Quantity}</td>
</tr>
</apex:repeat>
</table>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
If you still think you want Apex and component (maybe you want to filter some records out, maybe you want to have them sorted or have a reusable piece of markup to use in multiple emails)...
... the error you're getting is because ProductorderID is a single String (well, Id) variable. But you've used it in a repeat that expects a list/array. A reference to <apex:repeat value="{!ProductorderItems}" var="toi"> should call your getProductorderItems() and work better.

(ReactJS) I have mapping of account numbers with balances how to display it as a table in react?

I have a mapping with key being account number and value being account balance:
"balance": {
1: 1000,
2: 2000,
3: 3000
}
How do I display this content as a table in react?
account
balance
1
1000
2
2000
3
3000
Not a lot to go on here, for what exactly what you want, but I think ultimately you would do something like this in your render function.
<table>
<tr>
<th>account</th>
<th>balance</th>
</tr>
{Object.keys(balance).map((x) => (
<tr key={x}>
<td>{x}</td>
<td>{balance[x]}</td>
</tr>
))}
</table>
I just freehanded this, so probably can't copy-paste, but going off the information provided, I would think you are looking for something like this.

Loop through all fields of a Grails Objects of unknown properties

I currently have an ask to provide a "Lookup Table Management" interface to super users of an app I'm working on. They want to make changes to lookup tables that are used throughout the app (they won't be able to change IDs, of course).
I'd like to come up with a way to generically handle this without having to create a form for each LUT. Or if there is a plugin I don't know about that would be even better
All the LUTS are vastly different from each other in terms of fields. I'm trying to write a loop to loop through all the fields of a LUT object and then spit out the values for each object of that LUT.
Example:
DrinksLut
String name
Double price
Integer numSold
PetsLut
String name
Integer age
String breed
String size
Boolean fixed
I'm trying to provide a table output that lists the field properties as headers as well as the individual object values for rows depending on the LUT they choose to edit (Pets, Drinks, etc.)
Current Code
def resultList() {
def selectedLut = grailsApplication.getClassForName(params.lutFilter?.replace('class ', ''))
/* get field names, types and a "user friendly" name to use in the table header */
def objProps = new DefaultGrailsDomainClass(selectedLut)?.persistentProperties.collect {
[
name: it.name,
type: it.type,
friendlyName: Helper.splitCamelCase(it.name)
]
}
/* get all rows for the chosen LUT */
def results = selectedLut.getAll()
render(template: '../lookupTable/resultList', model:[results: results, objProps: objProps])
}
...
<table id="lutTable" class="col-xs-12">
<thead>
<tr>
<g:each in="${objProps}">
<th>${it.friendlyName}</th>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${results}">
<tr>
<td>${it}</td> <!-- this currently is the object of a LUT and I need to get each of its field properties and values to spit out here -->
</tr>
</g:each>
</tbody>
</table>
I'd greatly appreciate any assistance or ideas! The app s using Grails 2.5.5

Dynamic Text in selenium Webdriver

I m automating a web application and need help at one point.
When even i login to the web app. it ask for the user name(manager id) and then that manager id keep on flashing on home page. Manager id changes with different login credentials.
So i need to keep checking that Manager id.
<tr>
<td>
<center>
<table width="100%" align="center">
<tbody>
<tr/>
<!--comments: To link all the screens-->
<tr>
<tr class="heading3" align="center">
<td style="color: green">Manger Id : mngr8499</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
The manager ID as you can see in DOM is displayed as Manager Id: XXXX
So please help me how to locate these "XXXX" only, out of Manager id: XXXX
To my understanding after using getText() you get Manger Id : mngr8499 but you do not want every time Manager ID with the text and only ID. My Implementation would be to first copy the text in the any String variable and then using String.replaceAll() Method i can get the required text.
Below is the implementation of the same, Please let me know in-case if i am understanding the question wrong.
WebElement mngrid = driver.findElement(By.xpath("//tr[#class='heading3']/td"));
String actual_text = mngrid.getText();
actual_text= actual_text.replace("Manger Id :"," ");
System.out.println(actual_text);
From what I understand, I would use the .getText() method to get the 4 characters within the td using:
driver.findElement(By.cssSelector(".heading3 > td")).getText().substring(17);
If your concern is the refreshing of the html, consider using:
WebDriverWait wait = new WebDriverWait(driver, 10)
wait.until(ExpectedConditions.stalenessOf(By.cssSelector(".heading3 > td"));
When the previous value drops out of the DOM, you can use the new selector to get the refreshed value.

Edit and save multiple records in cakephp

In my cakephp app I have an Option model.
In my option/index view I display 2 options with inputs and radio button fields.
I want to update both of them, but I get a weird behaviour.
The option I alter doesn't get saved and instead a new option is inserted with the new value.
Here is my view
<h2 class='page-title' id='manage-options'>Opzioni</h2>
<?php echo $form->create(null, array('action'=>'index')); ?>
<table>
<tr>
<td><?= $options[0]['Option']['name']?></td>
<td><?= $form->radio(
$options[0]['Option']['id'],
array(
'1' => 'Sì',
'0' => 'No'),
array('default'=> $options[0]['Option']['value'], 'legend'=>false)
);?>
</td>
</tr>
<tr>
<td><?= $options[1]['Option']['name']?></td>
<td><?= $form->input($options[1]['Option']['id'],array('label'=>false,'value' => $options[1]['Option']['value'] ))?></td>
</tr>
</table>
<?php echo $form->submit('Salva'); ?>
<?php echo $form->end(); ?>
And my controller:
function index() {
if (!empty($this->data)) {
foreach($this->data['Option'] as $id => $value) :
$this->Option->id = $id;
$feedback = $this->Option->read();
$this->Option->saveField('value', $value);
endforeach;
$this->Session->setFlash('Opzioni aggiornate');
}
$this->Option->recursive = 0;
$this->set('options', $this->paginate());
}
Before posting here I spent two hours googling for answers and experimenting. I know about saveAll() and i have tried these solutions:
http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms
http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/
I have been tweaking my code to fit these patterns, but I got no results (oscillating between 'not working' and 'not working and I get an extra record'), so I decided to post my original code.
Can you help, indicating the most proper way to do this?
Cheeers,
Davide
The problem was with the data in the DB. The kind folks on the cakephp IRC channel called my attention to the fact that in most databases ID = 0 equals 'new record'. For some reason I had an option with ID 0, so when updating the underlaying mysql query actually created a new record.
Changed the IDs, problem fixed.
The main problem with your code that I see is that your fields, both the radio and the input, are being built with only an ID value as the first parameter. The correct "cake way" of building a field is having the first parameter be Model.fieldname, in your case I believe it would be $form->input('Option.id', array())?>
If you inspect the html generated by your code you will see the field name is data[id], and it should be data[Option][id] if you want to loop through $this->data['Option'] in your controller.
Try changing your code to include the Model.fieldname as the first parameter and then it should have the data submitted correctly to your controller.

Resources