And, to be specific, this would happen when trying to save an entity with relationship and the one being joined is fetched from memcached/redis or from other entity manager or you just populated it and not getting through Entity Manager
Solution to the doctrine error
Use the merge on EntityManager before you persist the object
Say you have an entity for pen and entity for color as well.. You want to save a new pen but you assigned a color from memcached object
In the request->process->response world of the MVC infrastructure, Symfony2 fits just perfect.
I was working on some data intensive site. All I want was to get the set of records from the mysql database and pass it to the front end through the controller. The front end wants the JSON format of the result set. The front end is getting the polished output of the twig
The problem
Front-end is not happy since it is getting quote-escaped version of JSON
The solution
Very very very simple. Rather than passing the json_encode(all_cars), where all_cars being the array of results coming from database or whatever it is, just pass the array it self
{{ all_cars | json_encode | raw }}
Yup, twig will not try to escape the quotes in this case since it is told to present raw.
Additional View
As you can see in this menu, both JSON and html views are together as combo. From design point of view this is not a good approach.
Better approach
You can bake the whole view right in the view and you might not want additional javascript logic on your view
OR
Have specific API to return 100% JSON response for the request like
some domain dot com/all/cars
By hitting this from, say your ajax call, you will be provided with json formatted list of all cars.
Since you are calling it from javascript, it will be directly coming to its home and no funny business would be there.
Then you will have another url call to load the page say:
some domain dot com/cars
where it will simply load the bare html format to the front-end and the front-end will know what to do with it.
Of course, this would have two trips and even more so it would be suitable for 80-90% of ajaxy sites..
It is almost a must to use mvn or any other build tool when dealing with java.
With mvn you can start a minimal simple all included java application and you run that too.. Just follow this. I will assume you are on linux or mac environment for this and also I am assuming you already have maven installed
Create a new folder and say mavenrocks
mkdir mavenrocks
Then add the following
mvn archetype:generate
What it does is, it will use a plugin to create a boilerplate application that holds basic src files along with standard directory structure.
if you do the listing on the created folder you will see:
If you got the above message when you try to add a new unique key on multiple column in mysql as
ALTER TABLE table_name ADD UNIQUE (col1, col2, col3);
Mysql would alert you if there is an already existing row in your table that is violating the newly added constraint.
Say you want to have uniqueness on col1 and col2 and assume col1 has value of “one” and col2 has value of “love”
Before this constraint you can have another row with the values of “one” and “love” for col1 and col2 respectively. But now that is not possible so you have to take care of that before applying
Then make sure you are binding the server with 0.0.0.0 as it is the main problem is being the default 127.0.0.1 for that, when yoiu are instantiation the server do the binding with 0.0.0.0
bin/server --binding=0.0.0.0
Basically the above would tell to listen to almost any ip. This has its own risk if you are doing it on public VM so caution shall be applied. Where as when it is 127.0.0.1, it will be listening to the local address only.
The above video will show the final assembled part of the motion detector.
I will try to have additional videos on how to setup each device from the scratch.
Below you will find the code that is used in the arduino part.
The basic part would be
– The arduino will detect the motion
– It will hit the API inside the raspberry pi
– The raspberry pi will log the motion being detected into the database.
After this, any other system can make the use of the data that has been logged into the database.
<pre>
/*
Web client
This sketch connects to a raspberry pi RoR websever
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
Modified for motion by Kaleb Woldearegay
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
byte ip[] = {192,168,2,4}; //shield's ip
byte geteway[] = {192,168,0,1};
// Set the static IP address to use if the DHCP fails to assign
IPAddress server(192,168,2,9); //pi's ip
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
//sensor and LED
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 7; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
//end sensor and LED
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//sensor and LED
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
//sensor and LED stuff
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
//Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 3000)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /increment");
client.println("Connection: close");
client.println();
client.stop();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
client.stop();
}
}
}
}
</pre>