我有一个关系OneToOne双向,配置如何级联persis,删除,但当我调用控制器removarPersonaFisicaAction时,这显示下一个错误:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using RTS;
public class QuestionMenu : MonoBehaviour {
public struct Question{
public string questionText;
public string answer;
public string solution1;
public string solution2;
public string solution3;
}
//public void QuestionObject(string QuestionText, string answer, string solution1, string solution2, string solution3){
//Debug.Log ("lol");
//}
public Question QuestionObject = new Question();
Canvas canvas;
public UserInput Player;
public Text QuestionText;
public Text Button1;
// Use this for initialization
void Start () {
canvas = GetComponent<Canvas>();
canvas.enabled = false;
ResourceManager.QuestionMenuOpen = false;
//instruction = GetComponent<Text>();
//Debug.Log (QuestionText);
Button1.text = "Crazy";
QuestionText.text = "Yo Bro";
}
// Update is called once per frame
void Update () {
////Debug.Log (QuestionObject.questionText);
if(ResourceManager.QuestionMenuOpen == true){
//QuestionObject.questionText = "bloo";
Pause ();
//QuestionText.text = "Question 1) Differentiate 3x\u2074 + 2x.";
//Button1.text = "12x\u00B3 + 2";
}
}
public void Pause (){
canvas.enabled = true;
Player.enabled = false;
Time.timeScale = 0.0f;
Cursor.visible = true;
//QuestionObject = Questions.QuestionObject;
Debug.Log (QuestionObject.questionText);
QuestionText.text = QuestionObject.questionText;
//ResourceManager.QuestionMenuOpen = true;
}
void Resume (){
Cursor.visible = false;
canvas.enabled = false;
Player.enabled = true;
Time.timeScale = 1.0f;
ResourceManager.QuestionMenuOpen = false;
}
}
这是我在实体中的配置: 第一个实体:
using UnityEngine;
using System.Collections;
public class QuestionScript : MonoBehaviour {
// Use this for initialization
void Start () {
QuestionMenu.QuestionObject.answer = "asdf";
}
// Update is called once per frame
void Update () {
}
}
第二实体:
extension Alamofire.Request {
func responseShop(queue: dispatch_queue_t? = nil, options: NSJSONReadingOptions = .AllowFragments, errorHandler:ProtocolWebServiceErrorHandler?, completionHandler: (ShopCallResult?, WebServiceErrorCode) -> Void ) -> Self {
//enter thread protected area...
objc_sync_enter(communicationLockObj)
return responseJSON() {
response in
switch response.result {
case .Success(let JSON):
//log json in verbose mode
log.verbose("\(JSON)")
//parse the returned json
let callResult = ShopCallResult(json: JSON)
//is it failed?
if callResult.statusCode.failed {
//call supplied error handler on the main thread
dispatch_async(dispatch_get_main_queue()) {
errorHandler?.handleWebServiceError(callResult.statusCode, errorText: callResult.statusCode.localizedText, request: self.request!, json: JSON)
completionHandler(callResult, callResult.statusCode)
}
//release the lock
objc_sync_exit(communicationLockObj)
//processing done!
return
}
//no error call handler on main thread
dispatch_async(dispatch_get_main_queue()) {
completionHandler(callResult, WebServiceErrorCode.OK)
}
//release the lock
objc_sync_exit(communicationLockObj)
case .Failure(let error):
//WARNING: cancelled is still error code 999?
//is it cancelled?
if error.code == -999 {
//just call the completion handler
dispatch_async(dispatch_get_main_queue()) {
//call handler with cancelled code
completionHandler(nil, WebServiceErrorCode.requestCancelled)
}
//release the lock
objc_sync_exit(communicationLockObj)
//stop furhter processing
return
}
//error with the json?
if error.code == Alamofire.Error.Code.JSONSerializationFailed .rawValue {
//call the error handler
dispatch_async(dispatch_get_main_queue()) {
errorHandler?.handleWebServiceError(WebServiceErrorCode.jsonNil, errorText: nil, request: self.request!, json: nil)
//call and supply org error
completionHandler(nil, WebServiceErrorCode.jsonNil)
}
//release the lock
objc_sync_exit(communicationLockObj)
//stop further processing
return
}
//must be some other kind of network error
dispatch_async(dispatch_get_main_queue()) {
log.error("\(error)")
//so call the error handler
errorHandler?.handleWebServiceError(WebServiceErrorCode.connectError, errorText : error.localizedDescription, request: self.request!, json: nil)
//call and supply org error
completionHandler(nil, WebServiceErrorCode.connectError)
}
//release the lock
objc_sync_exit(communicationLockObj)
}
}
}
}
eliminarPersonaFisicaAction:
An exception occurred while executing 'DELETE FROM entidad WHERE id = ?' with params [84]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`xxx`.`personafisica`, CONSTRAINT `FK_D55D20169B1A19BB` FOREIGN KEY (`id_entidad`) REFERENCES `entidad` (`id`))
答案 0 :(得分:6)
请注意,cascade={"persist", "remove"}
仅影响内部Doctrine2持久性和删除。它与数据库无关。
为了告诉DB明确添加&#39; onDelete&#39;您应在onDelete="CASCADE"
config。
JoinColumn
的列的属性
//code
/**
* @ORM\OneToOne(targetEntity="XXX\EntidadBundle\Entity\PersonaFisica", mappedBy="entidad", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="persona_fisica_id", referencedColumnName="id", onDelete="CASCADE")
**/
private $personaFisica;
//code
您不需要在两个表上指定JoinColumn
,只有一个表具有额外的id字段就足够了。根据我在代码中看到的内容,您应该将JoinColumn
添加到Entidad实体。这将使删除PersonaFisica时删除Entidad实体。