Category Archives: Programming

Checklist to run existing Symfony framework project on other machine

Migrating a Symfony 1.4 project from a machine to another machine would bring a hell sometimes. I have spent 4 days just to make it running. Here is some checklist for summary of what I am doing to get it running on another machine.
  • Check the version of Apache and PHP in production. You need to install the same in another machine. If it slightly different in PHP version, consider it acceptable.
  • Check installed pear packages in production by running command pear list -a
  • The database that symfony project is using. If it using MySQL I dont think it would give much trouble but if Oracle, you better need to install its client.
  • Oracle client isn’t complete without installing its php extension. Its called oci8. You can either install it using pear command.
  • As in Centos environment, oci8 isn’t friendly with SElinux. Things to do next is disable SElinux by running command setup
  • This is optional, disable suexec by stopping httpd service then rename /usr/sbin/suexec to something else. Personally I think this wont affect anything.
  • Make sure another machine is matched with symfony pre-requisite. Download its script check-configuration.php and run it.

Wrap Implode Array Elements in Quotes

$myArray = array('A', 'B', 'C');

echo "'" . implode("','", $myArray) . "'"; //Displays 'A', 'B', 'C'

echo implode(',', $myArray); //Displays A, B, C

CREATE TABLE LIKE

To duplicate table structure of existing table, we can use this command:

CREATE TABLE student_contact_deleted LIKE student_contact;

jQuery change image code snippet

$("#link").click(function() {
    $("#image").fadeOut(1000, function() {
        $("#image").attr("src",$("#link").attr("href"));
    }).fadeIn(1000);
    return false;
});

Useful code snippet for image change using jQuery. In this code image will change on click. Can be change to other event trigger like mouseover, mouseout.

source

Constant value in insert select

INSERT INTO linking_book_category (book_id, category_id)
SELECT book.id, 1 FROM books AS book;

More advance usage involving substring, trimming leading zeros as I try to migrate college’s intake from migration table.

INSERT INTO intakes (no,month,year,active,`status`,createdby,datecreated)
SELECT DISTINCT academicperiod, TRIM(LEADING '0' FROM SUBSTR(academicperiod,6,2)), SUBSTR(academicperiod,1,4), 1, 1, 1, NOW()
FROM _prestige_intake
WHERE academicperiod IN ('2006/01','2006/07','2007/01','2007/07','2008/01','2008/07','2009/01','2009/07','2010/01','2010/07')
ORDER BY academicperiod ASC

Update field with value from another table

UPDATE table_1 AS t1, table_2 AS t2
  SET
    t1.name = t2.title,
    t1.descr = t2.description,
    t1.price = t2.price,
    t1.available = t2.stock
  WHERE t1.productId = t2.sku

Taken from forum while browsing to find for solution. This query will took some time when dealing with thousands record in both table.

Merge rows as one field

GROUP_CONCAT(fieldname ORDER BY fieldname ASC SEPARATOR ',')

Full code usage example:

SELECT email, studentname,
GROUP_CONCAT(matrixno ORDER BY matrixno ASC SEPARATOR ',') AS matno,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

Code above used to find duplicates for email.